目前我在一个标签中有两个活动。在第二个活动中,我有自定义列表和自定义spinner
。微调器和listview
的数据是从服务器接收的....这很正常。因此,当我在第二个活动中点击spinner
时,应用程序崩溃,logcat
说:
08-13 11:54:55.908: E/AndroidRuntime(13555): java.lang.NullPointerException
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:328)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.AbsListView.obtainView(AbsListView.java:1474)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.ListView.onMeasure(ListView.java:1127)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.View.measure(View.java:8335)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.View.measure(View.java:8335)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-13 11:54:55.908: E/AndroidRuntime(13555): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.View.measure(View.java:8335)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.View.measure(View.java:8335)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.View.measure(View.java:8335)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.view.ViewRoot.handleMessage(ViewRoot.java:1892)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.os.Looper.loop(Looper.java:130)
08-13 11:54:55.908: E/AndroidRuntime(13555): at android.app.ActivityThread.main(ActivityThread.java:3835)
08-13 11:54:55.908: E/AndroidRuntime(13555): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 11:54:55.908: E/AndroidRuntime(13555): at java.lang.reflect.Method.invoke(Method.java:507)
08-13 11:54:55.908: E/AndroidRuntime(13555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
08-13 11:54:55.908: E/AndroidRuntime(13555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
08-13 11:54:55.908: E/AndroidRuntime(13555): at dalvik.system.NativeStart.main(Native Method)
我一直在看这些帖子 - > Android :WindowManager$BadTockenException on Spinner Click解决了我的第一个问题而Problem with spinner in custom list inside activity group没有给我什么。
所以我在这里设置视图和微调器以及我的自定义适配器:
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.atlaides, null);
this.setContentView(viewToLoad);
//setContentView(R.layout.atlaides);
sp = (Spinner) viewToLoad.findViewById(R.id.spinner1);
MySpiner sxspin = new MySpiner(this.getParent(), R.layout.c_spinner, android.R.id.text1, categories);
sxspin.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
this.sp.setAdapter(sxspin);
我的自定义微调器适配器如下所示:
private class MySpiner extends ArrayAdapter<String>{
public MySpiner(Context context, int resource, int textViewResourceId,
List<String> objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater pieputejs = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = pieputejs.inflate(R.layout.c_spinner, parent,false);
TextView text = (TextView)view.findViewById(R.id.textView1);
text.setText(categories.get(position));
//text.setTextColor(Color.GRAY);
//text.setShadowLayer(2.5f, 2, 2, Color.WHITE);
//text.setGravity(TextView.FOCUS_LEFT);
return view;
}
}
答案 0 :(得分:0)
NullPointerException
并非来自自定义适配器MySpinner
,它来自您在代码中其他位置的另一个简单ArrayAdapter
。在您提供给ArrayAdapter
的数据中,您有null
个值,这很可能会引发NullPointerException
。查看您传递的数据并从中删除null
值。