getMenuInflater导致空指针异常

时间:2019-06-24 22:06:15

标签: java android

在android应用上长按以弹出上下文菜单会导致空点异常崩溃。感谢所有帮助,如果需要更多代码,请告诉我,我不确定还有什么用。谢谢。

    mTitleEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                if (mTitleEditText != null) {
                    Log.d("mTitleEditText", " " + mTitleEditText.getText().toString());
                    SpellingsClient.myFunction(mTitleEditText);
                }
            }
        }
    });

///这是将editText传递到我的班级的方式。除了调出该菜单外,该课程运行得非常好,所以我认为问题不在这里。

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    Log.d("Tester", " menu " + menu + " v " + v + " menuInfo " + menuInfo + "  getMenuInflater() " +  getMenuInflater());
    MenuInflater menuInflater = getMenuInflater();

        menuInflater.inflate(R.menu.my_contextual_menu, menu);

        /*This adjusts menu items depending on if the word is inside of the personal dictionary*/
        if (itsInDict) {
            addOrRemove = "Remove from Dictionary";
        } else {
            addOrRemove = "Add to Dictionary";
        }

        /*These add in the menu items*/
        if (!itsInArray) {
            menu.add(0, 1, 1, addOrRemove);
        }
        menu.add(0, 2, 0, firstSuggestedWord);
        if (!originalWordTyped.equals("")) {
            menu.add(0, 3, 2, "Revert to: " + originalWordTyped);
        }
    }

//这是错误所在(getMenuInflator()导致崩溃)

E / Android运行时:致命异常:主要     流程:ws.xsoh.etar,PID:14389     java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.view.Window.getDecorView()”         在android.app.Activity.initWindowDecorActionBar(Activity.java:3303)         在android.app.Activity.getMenuInflater(Activity.java:4917)         在com.android.calendar.SpellingsClient.onCreateContextMenu(SpellingsClient.java:426)         在android.view.View.createContextMenu(View.java:14263)         在com.android.internal.view.menu.ContextMenuBuilder.showPopup(ContextMenuBuilder.java:102)         在com.android.internal.policy.DecorView.showContextMenuForChildInternal(DecorView.java:837)         在com.android.internal.policy.DecorView.showContextMenuForChild(DecorView.java:814)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:971)         在android.view.View.showContextMenu(View.java:7295)         在android.widget.TextView.showContextMenu(TextView.java:10930)         在android.view.View.performLongClickInternal(View.java:7209)         在android.view.View.performLongClick(View.java:7163)         在android.widget.TextView.performLongClick(TextView.java:12177)         在android.view.View.performLongClick(View.java:7181)         在android.view.View $ CheckForLongPress.run(View.java:27259)         在android.os.Handler.handleCallback(Handler.java:883)         在android.os.Handler.dispatchMessage(Handler.java:100)         在android.os.Looper.loop(Looper.java:214)         在android.app.ActivityThread.main(ActivityThread.java:7319)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)

1 个答案:

答案 0 :(得分:0)

您是直接从Activity或其他类中从Fragment调用吗?

如果不是来自活动,而是来自片段,请尝试以下操作:

MenuInflater inflater = getActivity().getMenuInflater();

相关问题