微调框中奇怪的NullPointerException

由于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.I追踪错误,以获取我的微调arrays适配器的查看方法。 这是我的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); // } 

问题消失。 有人有什么主意吗?

Solutions Collecting From Web of "微调框中奇怪的NullPointerException"

正如Peterdk 在这里描述的那样。 你不需要删除

机器人:ellipsize = “跑马灯”

他build议使用:

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

这解决了我的问题。 我猜Android 4.4正试图对充气布局进行严格的限制

所以我在TextView类的makeNewLayout方法中检查这行,发现这个exception发生在这一行final int height = mLayoutParams.height; 布局参数可能是空的..这行是在处理椭圆化长文本块所以我删除了XML文本视图属性android:ellipsize="marquee" ,然后问题真的消失..奇怪的奇怪

谢谢。 我通过从所有布局中删除android:ellipsize来消除崩溃。