android按钮onClick不转换为新的活动

问题描述:

因此,我对Android开发非常陌生,这可能是一个简单的问题,我错过了一件简单的事情。我有一个班,我有一个按钮,我试图点击一个按钮并转换到另一个活动。android按钮onClick不转换为新的活动

public class ContentProfile extends AppCompatActivity { 

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

    } 

    public void settingsPage(View v){ 
     Intent intent = new Intent(ContentProfile.this, ContentSettings.class); 
     startActivity(intent); 
    } 

} 

我想打开ContentSetting类。对于ContentProfile的XML是:

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Settings" 
    android:layout_alignParentRight="true" 
    android:id="@+id/button11" 
    android:onClick="settingsPage"/> 

和内容设置的样子:

public class ContentSettings extends AppCompatActivity { 


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

    } 


} 

我想从一个按钮,去到ContentProfile ContentSettings。我已阅读此文档here。我试图脱离这个文档,但我似乎无法弄清楚。我应该看另一份文件来解决这个问题吗?我可以忽略简单的事情吗?

编辑:这里是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.user.speed_read"> 

    <!-- To auto-complete the email text field in the login form with the user's emails --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.READ_PROFILE" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".LoginActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".ContentProfile" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="com.example.user.speed_read.ContentSettings" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".ContentSettings"></activity> 
    </application> 

</manifest> 

如果你想通过另一项活动单击该按钮时,您可以使用按钮点击收听。简单的方法来做到这一点。如果你这样做,你可以从你的xml中删除onclick。

final Button button = (Button) findViewById(R.id.button11); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(ContentProfile.this, ContentSettings.class); 
      startActivity(intent); 
     } 
    }); 
+0

我试过这个解决方案,当我单击按钮时,仍然没有任何反应。真的很困惑。 – Anderology

+0

您是否在onCreate方法中使用此代码? – yukocan

+0

是的。仍然不会做任何事情。 – Anderology

  1. 哪里是在内容配置文件类的onClickListener方法?您的onClickListener方法必须在其中包含settingsPage方法。例如:

final Button settingsButton = (Button) findViewById(R.id.settingsButton);

View.OnClickListener goToSettings = new View.OnClickListener(){ 
     @Override 
     public void onClick(View view) { 
      openSettings(); 
     } 
}; 

settingsButton.setOnClickListener(goToSettings);

  1. 必须在AndroidManifest.xml指定ContentSettings活性

  2. ContentSettings活动必须设置XML布局的正确内容名称。 activity_content_settings是XML布局的确切名称吗?

开始=>
+0

openSettings()在做什么?那只是我的settingsPage()方法?不知道你如何打破这一切。 View.OnClickListener进入onCreate吗? – Anderology

+0

也分2和3都是。它在AndroidManifest中完全一样XML – Anderology

+0

openSettings只是你的settingsPage方法,是的。这只是我在自己的应用程序中调用设置页面方法的名称。但它是一回事。 – joshgoldeneagle