- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
长按 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/
刚刚下载了 android studio,我正在使用 big nerd ranch 的 android 编程指南来学习技巧。 当您启动 android studio 时,这段代码已经在主 Activi
长按 Android 应用程序以调出上下文菜单会导致空点异常崩溃。感谢任何和所有帮助,让我知道是否需要更多代码,我不确定还有什么有用的。谢谢。 mTitleEditText.setOnFocu
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
我最近参加了 ABS4.0 潜水。但是,我似乎在使用 MenuInflater 时遇到了问题。 使用 getMenuInflater() 方法时,我收到一条错误消息:“类型不匹配:无法从 androi
我正在尝试在 fragment 中创建上下文菜单项。但是我遇到了 getMenuInflater() 方法的问题。它是未定义的。我已经导入了所有必要的包。谁能指出我在这里做错了什么? 这是我的代码:
我正在尝试在继承 Fragment 类的类中扩充菜单。这是我的 OnCreateOptionsMenu() 方法 - @Override public boolean OnCreateOptionsM
我正在学习 Android 并遵循简单的相机应用教程。我复制了一段代码,但出现错误,我不确定原因。 我使用的教程是, http://iwearshorts.com/blog/android-devel
我正在 fragment 中创建新的选项菜单,但在阅读之后 http://developer.android.com/resources/articles/avoiding-memory-leaks.
我想制作 map 。当我第一次尝试时,我遇到了问题。这是我的问题。 这是我的代码 @Override public boolean onCreateOptionsMenu(Menu menu) {
Bluetooth Application Starter Code 大家好!我正在做一个项目,需要访问Android的蓝牙API。我正在 Android Studio 中编码,并按照上面的链接开始。
为什么我必须两次告诉我的 Activity 它的布局应该是什么? protected void onCreate(Bundle savedInstanceState) { super.onCr
我正在开发 Android 应用程序,但在模板“主/详细流程”上实现共享按钮时遇到问题 当我在扩展 AppCompatActivity 的 ItemDetailActivity 上创建菜单时(但是,我
我是一名优秀的程序员,十分优秀!