2016-04-24 28 views
0

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

回答

0

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在你的活動中的方法。

希望得到這個幫助!

1

嘗試改變的參數private void buttonAbout1()看起來像private void buttonAbout1(View v)

1

您的buttonAbout1方法需要正確的簽名,意味着正確的參數。試試這條線代替:

更新:哦,我只是看到,你連接你的按鈕兩次onclick。你可以在代碼中執行它,也可以在xml中執行它。這是xml的解決方案。更改buttonAbout1方法

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