我试过实现简单的东西来启动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)时,应用会意外停止 请帮忙。
答案 0 :(得分:0)
getActionBar().setDisplayHomeAsUpEnabled(true);
您正试图在不存在ActionBar的Android版本上运行您的应用,该版本低于API 11。
答案 1 :(得分:0)
如果您的android:minSdkVersion
低于11,则方法getActionBar()
不可用,因此请尝试设置AndroidManifest.xml
android:minSdkVersion=11
或更高。
方法getActionBar()
仅在API 11之后可用。