Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird NullPointerException in Spinner

Since Android 4.4 KitKat, i have weird crashes in my applications. It seems that it has something to do with spinner. This stacktrace of bug i am receiving:

java.lang.NullPointerException
    at android.widget.TextView.makeNewLayout(TextView.java:6124)
    at android.widget.TextView.onMeasure(TextView.java:6400)
    at android.view.View.measure(View.java:16458)
    at android.widget.Spinner.setUpChild(Spinner.java:632)
    at android.widget.Spinner.makeView(Spinner.java:595)
    at android.widget.Spinner.getBaseline(Spinner.java:431)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1089)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
    at android.view.View.measure(View.java:16458)
    at com.android.internal.widget.ActionBarView.onMeasure(ActionBarView.java:1011)
    at android.view.View.measure(View.java:16458)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.widget.ActionBarContainer.onMeasure(ActionBarContainer.java:271)
    at android.view.View.measure(View.java:16458)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:254)
    at android.view.View.measure(View.java:16458)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
    at android.view.View.measure(View.java:16458)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4998)
    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:777)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    at dalvik.system.NativeStart.main(Native Method)

I have spinner navigation in actionbar and i think that it has something to do with it. I am using ABS.I have track down error to getView method of my spinner array adapter. Here is my getView method:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (LoginUtils.isLogged()) {
            items[Properties.NAVIG_ITEM_MY_PROFILE] = LoginUtils.getTextForAdapter();
        } else {
            items[Properties.NAVIG_ITEM_MY_PROFILE] = context.getString(R.string.userProfile);
        }
        View view = convertView;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.ab_spinner_item, null);
        }
        String text = items[position];
        TextView menu = (TextView) view.findViewById(android.R.id.text1);
        if (title == null) {
            menu.setText(text);
        } else {
            menu.setText(title);
        }
        return view;
    }

If i comment this lines:

//if (title == null) {
      menu.setText(text);
//    } else {
//        menu.setText(title);
//    }

problem disappears. Does anyone have any idea?

like image 433
Billda Avatar asked Nov 24 '13 16:11

Billda


People also ask

How do you fix a NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

Why NullPointerException is coming?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

How do I ignore Java Lang NullPointerException?

Answer: Some of the best practices to avoid NullPointerException are: Use equals() and equalsIgnoreCase() method with String literal instead of using it on the unknown object that can be null. Use valueOf() instead of toString() ; and both return the same result. Use Java annotation @NotNull and @Nullable.

What is NullPointerException example?

NullPointerException is a runtime exception and it is thrown when the application try to use an object reference which has a null value. For example, using a method on a null reference.


2 Answers

As Peterdk describe here . You dont need to remove

android:ellipsize="marquee"

He suggests using:

inflater.inflate(android.R.layout.simple_spinner_item, null);//WRONG inflater.inflate(android.R.layout.simple_spinner_item, parent, false);//GOOD

That fixed my issue. I guess Android 4.4 is trying to be strict about inflating layout

like image 164
Cuong Thai Avatar answered Sep 20 '22 03:09

Cuong Thai


So i check that line in TextView class in makeNewLayout method and found that this exception is happening on this line final int height = mLayoutParams.height; Layout params are probably null.. and this line is in block that handle ellipsizing long text .. so i removed that textView attribute in xml android:ellipsize="marquee" and then problem really disappears .. strange strange

like image 41
Billda Avatar answered Sep 20 '22 03:09

Billda