如何让我的按钮在Android Studio中打开第二个活动?

问题描述:

我已经阅读了许多其他论坛与他人的问题有关的问题,仍然无法让我的代码工作。我找不到任何代码错误,但我在后端(Java页面)和布局页面上发生错误。如何让我的按钮在Android Studio中打开第二个活动?

对于Java页面跟它:“在Android的父母或祖先上下文找不到方法buttonAbout1(查看):onclick属性”

而对于布局页面跟它:“方法“buttonAbout1 'in'GMOEd'签名不正确检查onClick XML属性中指定的方法是否在相关活动中声明“

我有我的代码如下所示。

预先感谢您!

主要活动(activity_gmoed)

<Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="About" 
     android:id="@+id/buttonAbout1" 
     android:background="#ffffff" 
     android:foregroundTint="#ffffff" 
     android:layout_below="@+id/textView2" 
     android:layout_alignLeft="@+id/textView2" 
     android:layout_alignStart="@+id/textView2" 
     android:onClick="buttonAbout1"/> 

我的Java页面的主要活动(GMOEd.Java)

public class GMOEd extends AppCompatActivity { 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gmoed); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    private void buttonAbout1() { 
     Button buttonAbout1 = (Button) findViewById(R.id.buttonAbout1); 
     assert buttonAbout1 != null; 
     buttonAbout1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(GMOEd.this,About2.class)); 
      } 
     }); 
     { 


     } 

清单页:

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

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".About2"></activity> 
     <!-- ATTENTION: This was auto-generated to add Google Play services to your project for 
      App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
    </application> 

</manifest> 

1. 首先,你需要做出buttonAbout1方法公开。

2.And那么你需要传递查看作为parameters.Like这

public void buttonAbout1(View v) { 
Button buttonAbout1 = (Button) findViewById(R.id.buttonAbout1); 
assert buttonAbout1 != null; 
    buttonAbout1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(GMOEd.this,About2.class)); 
     } 
    }); 
} 

这里有一个快速提示 避免yourself.Instead制作的onClick方法尝试使用机器人工作室的意图行动(ALT + ENTER)为您生成。当您在xml中添加android:onClick =“buttonAbout1”,然后按ALT + ENTER(确保您的光标位于onClick上),然后选择Create'buttonAbout1(View) '在GMOEd,这将crea在你的活动中的方法。

希望得到这个帮助!

尝试改变的参数private void buttonAbout1()看起来像private void buttonAbout1(View v)

您的buttonAbout1方法需要正确的签名,意味着正确的参数。试试这条线代替:

更新:哦,我只是看到,你连接你的按钮两次onclick。你可以在代码中执行它,也可以在xml中执行它。这是xml的解决方案。更改buttonAbout1方法

private void buttonAbout1(View v) { 
    startActivity(new Intent(GMOEd.this,About2.class)); 
}