gpt4 book ai didi

java - getMenuInflater 导致空指针异常

转载 作者:行者123 更新时间:2023-12-02 03:58:28 26 4
gpt4 key购买 nike

长按 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/AndroidRuntime:致命异常:main 进程: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( native 方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)

最佳答案

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

如果不是来自 Activity ,而是来自 fragment ,请尝试以下操作:

MenuInflater inflater = getActivity().getMenuInflater();

关于java - getMenuInflater 导致空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56744506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com