簡體   English   中英

如何獲得我的按鈕以在Android Studio中打開第二個活動?

[英]How to get my button to open a second activity in Android Studio?

我已經從其他人與該問題有關的問題中閱讀了許多其他論壇,但仍然無法使我的代碼正常工作。 我的代碼找不到任何錯誤,但后端(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>

嘗試更改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));
}

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(make確保光標位於onClick上),然后在GMOEd中選擇“ 創建'buttonAbout1(View)” ,這將在您的活動中創建方法。

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM