我已经从其他人与该问题相关的问题中阅读了许多其他论坛,但仍然无法使我的代码正常工作。我的代码找不到任何错误,但后端(Java页面)和布局页面却出现错误。

对于Java页面,它说:“在android:onClick属性的父级或祖先上下文中找不到方法buttonAbout1(View)”

对于布局页面,它说:“'GMOEd'中的方法'buttonAbout1'具有不正确的签名。检查在相关活动中是否声明了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然后需要将View作为参数传递

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));
        }
    });
}



  这是一个快速提示
  避免自己制作onClick方法。而是尝试使用android studio的意图动作(ALT + ENTER)为您生成。当您在xml中添加android:onClick =“ buttonAbout1”时,请按ALT + ENTER(确保光标位于onClick),然后在GMOEd中选择“创建'buttonAbout1(View)”,这将在您的活动中创建方法。


希望有帮助!

10-07 23:58