在Android中长按edittext时崩溃

时间:2014-08-12 05:19:01

标签: android android-edittext long-press

我有一个编辑文字。当我试图长时间按下edittext时,应用程序崩溃, 它给出了以下日志:

08-12 10:42:51.503: E/AndroidRuntime(838): FATAL EXCEPTION: main
08-12 10:42:51.503: E/AndroidRuntime(838): java.lang.NullPointerException
08-12 10:42:51.503: E/AndroidRuntime(838):  at  android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:302)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.support.v7.app.ActionBarActivityDelegate.getActionBarThemedContext(ActionBarActivityDelegate.java:208)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.support.v7.app.ActionBarActivityDelegateICS.onActionModeStarted(ActionBarActivityDelegateICS.java:195)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onActionModeStarted(ActionBarActivityDelegateICS.java:359)
08-12 10:42:51.503: E/AndroidRuntime(838):  at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2342)
08-12 10:42:51.503: E/AndroidRuntime(838):  at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2260)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:654)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:654)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:654)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:654)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:654)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:654)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.view.View.startActionMode(View.java:4519)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.widget.Editor.startSelectionActionMode(Editor.java:1524)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.widget.Editor.performLongClick(Editor.java:845)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.widget.TextView.performLongClick(TextView.java:8268)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.view.View$CheckForLongPress.run(View.java:17882)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.os.Handler.handleCallback(Handler.java:730)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.os.Looper.loop(Looper.java:137)
08-12 10:42:51.503: E/AndroidRuntime(838):  at android.app.ActivityThread.main(ActivityThread.java:5214)
08-12 10:42:51.503: E/AndroidRuntime(838):  at java.lang.reflect.Method.invokeNative(Native Method)
08-12 10:42:51.503: E/AndroidRuntime(838):  at java.lang.reflect.Method.invoke(Method.java:525)
08-12 10:42:51.503: E/AndroidRuntime(838):  at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
08-12 10:42:51.503: E/AndroidRuntime(838):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-12 10:42:51.503: E/AndroidRuntime(838):  at dalvik.system.NativeStart.main(Native Method)  

我不知道为什么会这样。

5 个答案:

答案 0 :(得分:0)

here

尝试此操作
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ( keyCode == KeyEvent.KEYCODE_MENU ) {
        // do nothing
        return true;
    }
    return super.onKeyDown(keyCode, event);
} 

答案 1 :(得分:0)

也许你需要getSupportActionbar()而不是getActionbar()

答案 2 :(得分:0)

我已经研究过这个问题,发现这个问题是android OS的问题,并且在values/styles删除下面的代码中解决了这个问题:

<item name="android:fontFamily">every things here</item>

答案 3 :(得分:0)

IF您的EditTextTextInputLayout包裹起来,这可能对您有帮助

TextInputLayout分配了theme,请确保其父级为

parent="Widget.Design.TextInputLayout"

这是我的问题,更换父母后,它开始正常工作。

答案 4 :(得分:0)

下面是对我有用的...

1)如果您的EditText用 TextInputLayout 包装,则将以下属性添加到您的Edittext

  

android:longClickable = false

2)如果您为EditText使用自定义样式,则将以下属性添加到自定义样式代码中。

<item name="android:longClickable">false</item>

希望这会有所帮助。