1

申し訳ありませんが、私はアンドロイド開発に非常に慣れていないため、ユーザーがボタンを押すとActivity2が開始され、同様にユーザーがActivity2のキャンセルボタンを押すと元のアクティビティに戻ることをワークアウトしたいと思います. .

アプリの作成に関する本を参照しましたが、機能しないようです。コーディングは簡単で、次のように見えます。

public class NameIndex extends Activity 
{
       // called when the activity is first created
       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.name_index);

           public button_cancel_click (View view) { /////// <-- ERROR AT THIS LINE
               Intent intent = new Intent (this, GameIndex.class);
               startActivity(intent);
           }
       } // end method onCreate
}

xml レイアウトは次のとおりです。

    <TableRow android:id="@+id/tableRow1" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"
      android:paddingBottom="10dp"
      android:paddingTop="10dp"      
      android:layout_span="2" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="1"
            android:layout_weight="1"
            android:onClick="button_cancel_click"            
            android:text="Cancel" />     
    </TableRow>          

Eclipse は、上記のエラー行を「button_cancel_click をタイプに解決できません」と報告します。

view "パラメータ ビューの修飾子が正しくありません。final のみが許可されています"。

これはどのように解決できますか?

4

2 に答える 2

4

OnCreate関数本体にbutton_cancel_click関数の定義を書いているので、次のように修正してください。

public void onCreate(Bundle savedInstanceState)
{
.
.
.
}
public button_cancel_click (View view) 
{
.
.
}
于 2012-10-02T10:25:03.947 に答える
0

リスナーは次のように追加する必要があります。

super.onCreate(savedInstanceState); 
setContentView(R.layout.name_index);
Button button = (Button ) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {                       
                    public void onClick(View v) {
                        Intent intent = new Intent (this, GameIndex.class);
           startActivity(intent);
                    }
                });
于 2012-10-02T10:20:26.223 に答える