2016-04-24 5 views
0

この問題に関する他の質問から他の多くのフォーラムを読んだことがあります。私は自分のコードに間違ったものは見つけられませんが、バックエンド(Javaページ)とレイアウトページでエラーが発生しています。 JavaのページについてはAndroidスタジオで2番目のアクティビティを開くためにボタンを取得するにはどうすればよいですか?

言っている:「:のonClick属性アンドロイドの親や先祖コンテキスト方法buttonAbout1(ビュー)を見つけることができませんでした」

を、レイアウト・ページのためにそれを言っている:「メソッド「buttonAbout1間違った署名を持っている 『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. まずに変更します。

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メソッドを作る簡単なヒント 避けてはアンドロイドスタジオの意思アクションを使用してみてくださいです追加you.Whenのために生成する(ALT + ENTERを)アンドロイド:onClickのは= "buttonAbout1" XMLで、押しALT +(ビュー)(onClickの上にあることを確認カーソルを作る)入力し、」のbuttonAbout1の作成]を選択します'GMOEdとそれはクレアあなたの活動の中の方法を教えてください。

希望!

1

のパラメータを変更してみてくださいprivate void buttonAbout1()のように見えるprivate void buttonAbout1(View v)

1

あなたのbuttonAbout1メソッドは、正しい署名を必要とする、正しいパラメータを意味します。あなたはonclickのに二回あなたのボタンを接続し、ああ、私は以下を参照してください。

更新:代わりにこの行を試してみてください。あなたはコードでそれを行うか、xmlでそれを行います。ここではxmlの解があります。あなたはbuttonAbout1メソッドを公開する必要がbuttonAbout1方法

private void buttonAbout1(View v) { 
    startActivity(new Intent(GMOEd.this,About2.class)); 
} 
関連する問題