奇怪的 NullPointerException in Spinner

自Android 4.4 KitKat以来,我的应用程序中出现了奇怪的崩溃。它似乎与微调器有关。我收到的这个错误堆栈跟踪:

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)

我在动作栏中有微调器导航,我认为它与它有关。我正在使用ABS。我已经跟踪错误到我的微调器数组适配器的getView方法。这是我的getView方法:

@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 (title == null) {
      menu.setText(text);
//    } else {
//        menu.setText(title);
//    }

问题消失了。有人知道吗?


答案 1

正如彼得德克在这里描述的那样。您无需移除

android:ellipsize=“marquee”

他建议使用:

inflater.inflate(android.R.layout.simple_spinner_item,null);//ERROR inflater.inflate(android.R.layout.simple_spinner_item,父级,假);//GOOD

这解决了我的问题。我猜Android 4.4正试图严格要求膨胀布局


答案 2

所以我在 makeNewLayout 方法中检查了 TextView 类中的该行,发现此异常发生在此行布局参数可能为 null。并且此行位于处理省略长文本的块中。所以我在xml中删除了那个textView属性,然后问题真的消失了。奇怪 奇怪final int height = mLayoutParams.height;android:ellipsize="marquee"


推荐