Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...