imagebuttonで新しいアクティビティを開始するために簡単なものを実装しようとしました。「Dealsofday」は私のimagebutton名です...これが私の1)mainactiviy.javaです。
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtondod();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void addListenerOnButtondod() {
final Context context = this;
ImageButton btndod = (ImageButton) findViewById(R.id.Dealsofday);
btndod.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,Spldeals.class);
startActivity(intent);
}
});
}
}
2)Spldeals.javaは以下のとおりです
public class Spldeals extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spldeals);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_spldeals, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
私のログは以下のようになります
03-08 00:22:38.697: D/dalvikvm(477): GC_EXTERNAL_ALLOC freed 678 objects / 52432 bytes in 87ms
03-08 00:22:42.777: I/dalvikvm(477): Could not find method com.example.test.Spldeals.getActionBar, referenced from method com.example.test.Spldeals.onCreate
03-08 00:22:42.777: W/dalvikvm(477): VFY: unable to resolve virtual method 3320: Lcom/example/test/Spldeals;.getActionBar ()Landroid/app/ActionBar;
03-08 00:22:42.788: D/dalvikvm(477): VFY: replacing opcode 0x6e at 0x0009
03-08 00:22:42.797: D/dalvikvm(477): VFY: dead code 0x000c-0011 in Lcom/example/test/Spldeals;.onCreate (Landroid/os/Bundle;)V
03-08 00:22:42.878: D/AndroidRuntime(477): Shutting down VM
03-08 00:22:42.878: W/dalvikvm(477): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-08 00:22:42.897: E/AndroidRuntime(477): FATAL EXCEPTION: main
03-08 00:22:42.897: E/AndroidRuntime(477): java.lang.NoSuchMethodError: com.example.test.Spldeals.getActionBar
03-08 00:22:42.897: E/AndroidRuntime(477): at com.example.test.Spldeals.onCreate(Spldeals.java:15)
03-08 00:22:42.897: E/AndroidRuntime(477): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 00:22:42.897: E/AndroidRuntime(477): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-08 00:22:42.897: E/AndroidRuntime(477): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-08 00:22:42.897: E/AndroidRuntime(477): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-08 00:22:42.897: E/AndroidRuntime(477): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-08 00:22:42.897: E/AndroidRuntime(477): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 00:22:42.897: E/AndroidRuntime(477): at android.os.Looper.loop(Looper.java:123)
03-08 00:22:42.897: E/AndroidRuntime(477): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-08 00:22:42.897: E/AndroidRuntime(477): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:22:42.897: E/AndroidRuntime(477): at java.lang.reflect.Method.invoke(Method.java:521)
03-08 00:22:42.897: E/AndroidRuntime(477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-08 00:22:42.897: E/AndroidRuntime(477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-08 00:22:42.897: E/AndroidRuntime(477): at dalvik.system.NativeStart.main(Native Method)
最初の画面で問題なく、正しく読み込まれますが、画像ボタン(Dealsofday)をクリックすると、アプリが予期せず停止します。助けてください。