Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException at ActionBarImplICS (using app compat) on long-press

NullPointerException when long-press over field

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

<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)
like image 928
Benn Sandoval Avatar asked May 10 '14 21:05

Benn Sandoval


2 Answers

The ActionBar is null. That's because you're using an activity without a title (and thus also without an actionbar):

requestWindowFeature(Window.FEATURE_NO_TITLE);

There are two ways to fix it.

Simple:
Make YourActivity NOT extends ActionBarActivity but normal Activity.

More difficult:
Make transparent ActionBar with no title, no icon, no home button etc. How to do this? First, define ActionBar style and new theme that extends 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>

Next, set this theme to your fullscreen activity:

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

Finally, add following lines to YourFullscreenActivity#onCreate method:

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

I hope it will help.

like image 88
erakitin Avatar answered Nov 09 '22 09:11

erakitin


I think I have a better solution to this issue.

Remove requestWindowFeature(Window.FEATURE_NO_TITLE); from activity and <item name="android:windowNoTitle">true</item> from activity theme.

To your activity code add:

To onCreate() method

View actionBar = getActionBarView();
if (actionBar != null) {
    actionBar.setVisibility(View.GONE);
}

To activity class:

public View getActionBarView() {
    Window window = getWindow();
    View v = window.getDecorView();
    int resId;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        resId = getResources().getIdentifier(
                "action_bar_container", "id", getPackageName());
    } else {
        resId = Resources.getSystem().getIdentifier(
                "action_bar_container", "id", "android");
    }
    if (resId != 0) {
        return v.findViewById(resId);
    } else {
        return null;
    }
}

@Override
public void onSupportActionModeStarted(ActionMode mode) {
    super.onSupportActionModeStarted(mode);
    View actionBar = getActionBarView();
    if (actionBar != null) {
        actionBar.setVisibility(View.VISIBLE);
    }
}

@Override
public void onSupportActionModeFinished(ActionMode mode) {
    super.onSupportActionModeFinished(mode);
    View actionBar = getActionBarView();
    if (actionBar != null) {
        actionBar.setVisibility(View.GONE);
    }
}

This is remove action bar and at the same time allows you to manipulate text in edit text with action mode buttons.

Cheers

like image 32
MikhailKrishtop Avatar answered Nov 09 '22 09:11

MikhailKrishtop