android – 在长按时在ActionBarImplICS(使用app compat)的NullPointerException

前端之家收集整理的这篇文章主要介绍了android – 在长按时在ActionBarImplICS(使用app compat)的NullPointerException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
长按字段时出现NullPointerException
compile 'com.android.support:appcompat-v7:19.1.+'

XML

<EditText
    android:id="@+id/field"
    style="@style/Field"
    android:textSize="20sp"
    android:hint="@string/hint"
    android:nextFocusDown="@+id/other"/>

样式

<style name="Field" parent="Theme.AppCompat.Light">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/blue</item>
    <item name="android:singleLine">true</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:inputType">textCapCharacters|textNoSuggestions</item>
    <item name="android:imeOptions">actionNext</item>
    <item name="android:popupBackground">@color/white</item>
</style>

logcat的

java.lang.NullPointerException
     at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:302)
     at android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20)
     at android.support.v7.app.ActionBarActivityDelegate.getActionBarThemedContext(ActionBarActivityDelegate.java:208)
     at android.support.v7.app.ActionBarActivityDelegateICS.onActionModeStarted(ActionBarActivityDelegateICS.java:195)
     at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onActionModeStarted(ActionBarActivityDelegateICS.java:359)
     at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2437)
     at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2362)
     at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
     at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
     at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
     at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
     at android.view.View.startActionMode(View.java:4554)
     at android.widget.Editor.startSelectionActionMode(Editor.java:1551)
     at android.widget.Editor.performLongClick(Editor.java:859)
     at android.widget.TextView.performLongClick(TextView.java:8373)
     at android.view.View$CheckForLongPress.run(View.java:18441)
     at android.os.Handler.handleCallback(Handler.java:733)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5102)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
     at dalvik.system.NativeStart.main(Native Method)

解决方法

ActionBar为null.那是因为你正在使用没有标题的活动(因此也没有动作栏):
requestWindowFeature(Window.FEATURE_NO_TITLE);

有两种方法可以解决它.

简单:
Make YourActivity不会扩展ActionBarActivity,而是普通的Activity.

更加困难:
制作透明的ActionBar,没有标题,没有图标,没有主页按钮等.怎么做?首先,定义ActionBar样式和扩展YourAppTheme的新主题

<!-- Your App Theme-->
<style name="YourAppTheme.Light" parent="@style/Theme.AppCompat.Light">
    <...>
</style>

<!-- Transparent ActionBar Style -->
<style name="YourAppTheme.Light.ActionBar.Transparent" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@android:color/transparent</item>
    <item name="background">@android:color/transparent</item>
</style>

<!-- Fullscreen Activity Theme -->
<style name="YourAppTheme.FullScreen.Light" parent="@style/YourAppTheme.Light">
    <item name="android:actionBarStyle">@style/YourAppTheme.Light.ActionBar.Transparent</item>
    <item name="actionBarStyle">@style/YourAppTheme.Light.ActionBar.Transparent</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="windowActionBarOverlay">true</item>
</style>

接下来,将此主题设置为全屏活动:

<activity
    android:name="your.package.name.YourFullscreenActivity"
    android:label="@string/app_name"
    android:theme="@style/YourAppTheme.FullScreen.Light" />

最后,在YourFullscreenActivity#onCreate方法添加以下行:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);

我希望它会有所帮助.

原文链接:https://www.f2er.com/android/314247.html

猜你在找的Android相关文章