2016-07-10 37 views
0

我无法从一个页面移动到另一个页面。
按钮不起作用,如果我按下它什么都没有发生。
你能解释一下为什么吗?
我只想转到页面activity_main.xmlpagetwo.xml,为了做到这一点,我尝试更改Activity如何更改当前页面,从activity_main.xml切换到pagetwo.xml

我的网页:

MainActivity:

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

    ... 

    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ... 

    } 
} 


class open extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
        Intent i= new Intent("call"); 
        startActivity(i); 
      } 
     }); 
    } 

} 

电话:

public class call extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.pagetwo); 
    } 
} 

清单:

... 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".call" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar" > 
     <intent-filter > 
      <action android:name="android.intent.action.PICK_ACTIVITY"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 

pagetwo:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="Finally" 
    android:textAppearance="?android:attr/textAppearanceLarge"/> 

</RelativeLayout> 

activity_main:

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView" 
    android:layout_marginTop="6dp" 
    android:layout_centerHorizontal="true" 
    android:text="page two" /> 
</RelativeLayout> 

再见!

+0

https://developer.android.com/training/basics/firstapp/starting-activity.html – tachyonflux

+0

你的代码过于复杂,并且显示你不了解活动(你不应该在这里超过2,而且不应该混合使用Activity和AppCompatActivity)。我建议阅读教程和文档,给你解决这个问题的代码会让你受到更多的伤害。 –

+0

谢谢你,我读了所有,现在我解决了我的问题。 – User

回答

0

更改密码,并检查

Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
        Intent i= new Intent(MainActivity.class, call.class); 
        startActivity(i); 
      } 
     }); 
0

您没有正确使用该意图。请检查下面的源代码类像下面open

class open extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
        Intent i= new Intent(this, call.class); 
        startActivity(i); 
      } 
     }); 
    } 
} 
相关问题