0

目前我在一个标签中有两个活动。在第二个活动中,我有自定义列表和自定义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这解决了我的第一个问题和活动组内自定义列表中的微调器问题,但没有给我任何帮助。

所以这是我设置视图和微调器以及我的自定义适配器的方法:

  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;
    }       
}
4

1 回答 1

0

NullPointerException不是来自自定义适配器MySpinner,而是来自ArrayAdapter代码中其他地方的另一个简单的适配器。在您提供给您的数据中,ArrayAdapter您有null值,这就是最有可能抛出NullPointerException. 查看您传递的数据并null从中删除值。

于 2012-08-13T09:29:28.287 回答