尝试添加设置菜单但应用程序不断崩溃

时间:2013-03-08 21:58:54

标签: java android eclipse adt

我正在尝试添加设置菜单(根据教程),但应用程序一直在崩溃,我不知道为什么。我相信我正确地遵循了指示,但它一直都失败了,所以我很确定我忽视或不理解简单的事情。

package com.example.linkingmanager;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.content.Intent;
import android.view.View; 
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class App2Activity extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}

 public void imageClick(View view) {  
  //Implement image click function
     Intent intent = new Intent(App2Activity.this, AppActivity.class);
     App2Activity.this.startActivity(intent);
 }  
 @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }

    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
        case R.id.menu_bookmark:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_save:
            Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_search:
            Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_share:
            Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_delete:
            Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_preferences:
            Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected",     Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }    

}

}
;

logcat的

03-08 21:54:51.327: E/Trace(1962): error opening trace file: No such file or directory (2)
03-08 21:54:52.015: D/dalvikvm(1962): GC_FOR_ALLOC freed 40K, 7% free 2456K/2640K, paused 52ms, total 56ms
03-08 21:54:52.075: I/dalvikvm-heap(1962): Grow heap (frag case) to 5.831MB for 3456016-byte allocation
03-08 21:54:52.195: D/dalvikvm(1962): GC_FOR_ALLOC freed 2K, 4% free 5828K/6016K, paused 118ms, total 119ms
03-08 21:54:52.285: D/dalvikvm(1962): GC_CONCURRENT freed <1K, 4% free 5828K/6016K, paused 10ms+5ms, total 90ms
03-08 21:54:52.955: D/libEGL(1962): loaded /system/lib/egl/libEGL_emulation.so
03-08 21:54:53.102: D/(1962): HostConnection::get() New Host Connection established 0x2a1546b0, tid 1962
03-08 21:54:53.177: D/libEGL(1962): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-08 21:54:53.185: D/libEGL(1962): loaded /system/lib/egl/libGLESv2_emulation.so
03-08 21:54:53.255: W/EGL_emulation(1962): eglSurfaceAttrib not implemented
03-08 21:54:53.275: D/OpenGLRenderer(1962): Enabling debug mode 0
03-08 21:54:56.455: W/EGL_emulation(1962): eglSurfaceAttrib not implemented
03-08 21:54:56.525: D/AndroidRuntime(1962): Shutting down VM
03-08 21:54:56.525: W/dalvikvm(1962): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-08 21:54:56.595: E/AndroidRuntime(1962): FATAL EXCEPTION: main
03-08 21:54:56.595: E/AndroidRuntime(1962): java.lang.Error: Unresolved compilation problem: 
03-08 21:54:56.595: E/AndroidRuntime(1962):     menu cannot be resolved or is not a field
03-08 21:54:56.595: E/AndroidRuntime(1962):     at com.example.linkingmanager.App2Activity.onCreateOptionsMenu(App2Activity.java:33)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:407)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:769)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:201)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.view.Choreographer.doFrame(Choreographer.java:531)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.os.Handler.handleCallback(Handler.java:725)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.os.Looper.loop(Looper.java:137)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at java.lang.reflect.Method.invoke(Method.java:511)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-08 21:54:56.595: E/AndroidRuntime(1962):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您正尝试在此处展开​​布局而不是菜单:menuInflater.inflate(R.layout.menu, menu);

您必须提供描述菜单元素的Menu XML resource