android.view.InflateException,当我尝试打开菜单时Android应用程序崩溃!

时间:2011-04-19 13:42:15

标签: android

我为Android 2.2编写了一个游戏(使用GLSurfaceView)应用程序。我尝试打开菜单时出现以下错误:

android.view.InflateException:二进制XML文件行#17:错误输出类com.android.internal.view.menu.IconMenuView

---------堆栈追踪---------

android.view.LayoutInflater.createView(LayoutInflater.java:513)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
android.view.LayoutInflater.inflate(LayoutInflater.java:385)
android.view.LayoutInflater.inflate(LayoutInflater.java:320)
com.android.internal.view.menu.MenuBuilder$MenuType.getMenuView(MenuBuilder.java:197)
com.android.internal.view.menu.MenuBuilder.getMenuView(MenuBuilder.java:323)
com.android.internal.policy.impl.PhoneWindow.initializePanelContent(PhoneWindow.java:840)
com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:417)
com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:603)
com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1319)
com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1648)
android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:123)
android.app.ActivityThread.main(ActivityThread.java:4627)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:521)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
dalvik.system.NativeStart.main(Native Method)

这种情况发生在某些手机上(我的错误永远不会发生):

我的代码是:

public class FinalKarate extends Activity
{
    private KarateGLSurfaceView view = null;
    PowerManager.WakeLock wl = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        final PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");           

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        view = new KarateGLSurfaceView(this);
        setContentView(view);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.game_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle item selection
        switch (item.getItemId())
        {
            case R.id.item01:
                if ((view != null) && (view.isPlayingGame()))
                {
                    final boolean alreadyInPause = view.isInPause();
                    if (!alreadyInPause)
                        view.pauseGame();
                    new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle(R.string.abort_game)
                    .setMessage(R.string.abort_request)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        //@Override
                        public void onClick(DialogInterface dialog, int which) {
                            view.abortGame();    
                        }
                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        //@Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (!alreadyInPause)
                                view.resumeGame();    
                        }
                    })
                    .show();
                }
                return true;

            case R.id.item02:
                if (view != null)
                {
                    if (view.isInPause())
                        view.resumeGame();
                    else
                        view.pauseGame();
                }
                return true;

            case R.id.item03:
                if (view != null)
                    view.toggleSimplifiedMode();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

我错了什么?请帮忙!

P.s:xml文件如下(n.b:我已将<>替换为[]并在论坛中发布):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item01" android:title="@string/abort_game"></item>
    <item android:id="@+id/item02" android:title="@string/pause_game"></item>
    <item android:title="@string/simplyMode" android:id="@+id/item03"></item>
</menu>

其他信息:

--------- Cause ---------

java.lang.reflect.InvocationTargetException

    com.android.internal.view.menu.IconMenuView.(IconMenuView.java:140)
    java.lang.reflect.Constructor.constructNative(Native Method)
    java.lang.reflect.Constructor.newInstance(Constructor.java:446)
    android.view.LayoutInflater.createView(LayoutInflater.java:500)
    android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
    android.view.LayoutInflater.inflate(LayoutInflater.java:385)
    android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    com.android.internal.view.menu.MenuBuilder$MenuType.getMenuView(MenuBuilder.java:197)
    com.android.internal.view.menu.MenuBuilder.getMenuView(MenuBuilder.java:323)
    com.android.internal.policy.impl.PhoneWindow.initializePanelContent(PhoneWindow.java:840)
    com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:417)
    com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:603)
    com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1319)
    com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1648)
    android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
    android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
    android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
    android.os.Handler.dispatchMessage(Handler.java:99)
    android.os.Looper.loop(Looper.java:123)
    android.app.ActivityThread.main(ActivityThread.java:4627)
    java.lang.reflect.Method.invokeNative(Native Method)
    java.lang.reflect.Method.invoke(Method.java:521)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    dalvik.system.NativeStart.main(Native Method)
-------------------------------

1 个答案:

答案 0 :(得分:0)

对于像他这样有问题的其他人。发生这种情况的最大原因是因为他没有在手机内保存android.R.drawable.ic_dialog_alert。他只是打电话给它。如果不存在,inflater将为该drawable发出一个调用目标异常。