我正在尝试从 MenuItem 开始一项活动,但是每次都失败。我确定我忽略了一些简单的事情。
附言
非常感谢解释我做错了什么而不是更新代码!
- 阿曼尼·斯旺
来源:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AppActivity extends Activity {
private Button button1,button2,button3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
}
public void openDialog(View v){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(AppActivity.this)
.setTitle("Settings Menu")
.setMessage("Delete Edit or Link?")
.setCancelable(false)
.setNeutralButton("Edit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Intent intentApp2Activity = new Intent(AppActivity.this, User1Settings.class);
startActivity(intentApp2Activity);
}
})
.setPositiveButton("Link",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Intent intentApp2Activity = new Intent(AppActivity.this, User1.class);
startActivity(intentApp2Activity);
}
})
.setNegativeButton("Delete",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
Intent intentApp2Activity = new Intent(AppActivity.this, User1.class);
startActivity(intentApp2Activity);
}
}
LOGCAT
03-14 01:51:12.781: D/dalvikvm(1982): GC_FOR_ALLOC freed 36K, 7% free 2467K/2648K, paused 51ms, total 56ms
03-14 01:51:12.831: I/dalvikvm-heap(1982): Grow heap (frag case) to 6.061MB for 3686416-byte allocation
03-14 01:51:12.951: D/dalvikvm(1982): GC_FOR_ALLOC freed 2K, 3% free 6065K/6252K, paused 119ms, total 119ms
03-14 01:51:13.131: D/dalvikvm(1982): GC_CONCURRENT freed <1K, 3% free 6065K/6252K, paused 7ms+6ms, total 178ms
03-14 01:51:14.391: D/libEGL(1982): loaded /system/lib/egl/libEGL_emulation.so
03-14 01:51:14.487: D/(1982): HostConnection::get() New Host Connection established 0x2a152778, tid 1982
03-14 01:51:14.711: D/libEGL(1982): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-14 01:51:14.731: D/libEGL(1982): loaded /system/lib/egl/libGLESv2_emulation.so
03-14 01:51:14.981: W/EGL_emulation(1982): eglSurfaceAttrib not implemented
03-14 01:51:15.000: D/OpenGLRenderer(1982): Enabling debug mode 0
03-14 01:51:16.871: D/dalvikvm(1982): GC_FOR_ALLOC freed 12K, 3% free 6125K/6280K, paused 53ms, total 60ms
03-14 01:51:16.941: I/dalvikvm-heap(1982): Grow heap (frag case) to 9.634MB for 3686416-byte allocation
03-14 01:51:17.161: D/dalvikvm(1982): GC_CONCURRENT freed 2K, 2% free 9722K/9884K, paused 81ms+17ms, total 227ms
03-14 01:51:19.020: W/EGL_emulation(1982): eglSurfaceAttrib not implemented
03-14 01:51:42.030: D/AndroidRuntime(1982): Shutting down VM
03-14 01:51:42.040: W/dalvikvm(1982): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-14 01:51:42.160: E/AndroidRuntime(1982): FATAL EXCEPTION: main
03-14 01:51:42.160: E/AndroidRuntime(1982): java.lang.Error: Unresolved compilation problem:
03-14 01:51:42.160: E/AndroidRuntime(1982): This method must return a result of type boolean
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.example.linkingmanager.AppActivity.onOptionsItemSelected(AppActivity.java:84)
03-14 01:51:42.160: E/AndroidRuntime(1982): at android.app.Activity.onMenuItemSelected(Activity.java:2548)
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
03-14 01:51:42.160: E/AndroidRuntime(1982): at android.view.View.performClick(View.java:4204)
03-14 01:51:42.160: E/AndroidRuntime(1982): at android.view.View$PerformClick.run(View.java:17355)
03-14 01:51:42.160: E/AndroidRuntime(1982): at android.os.Handler.handleCallback(Handler.java:725)
03-14 01:51:42.160: E/AndroidRuntime(1982): at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 01:51:42.160: E/AndroidRuntime(1982): at android.os.Looper.loop(Looper.java:137)
03-14 01:51:42.160: E/AndroidRuntime(1982): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-14 01:51:42.160: E/AndroidRuntime(1982): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 01:51:42.160: E/AndroidRuntime(1982): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-14 01:51:42.160: E/AndroidRuntime(1982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-14 01:51:42.160: E/AndroidRuntime(1982): at dalvik.system.NativeStart.main(Native Method)