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