在listView中创建微调器会导致应用程序崩溃

时间:2014-12-24 12:50:28

标签: android spinner

我正在创建一个列表视图,我在那里动态地给行充气,基于某个变量,有时行是一个复选框,有时候是一个微调器。

活动可以显示复选框没问题,但应用程序在尝试创建微调器时崩溃。

这是我的代码:

        DetailsAdapter(Context c,String[] titles,Map<String,List<String>> details,List<String> types)
    {
        super(c,R.layout.details_single_row,R.id.text_view,titles);
        this.context=c;
        this.titleArray=titles;
        this.details=details;
        this.types=types;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        String mCurrentType=types.get(position);
        if (mCurrentType.contains("checkbox"))
        {
            View row = inflater.inflate(R.layout.details_single_row, parent, false);
            TextView myTitle=(TextView) row.findViewById(R.id.text_view);
            ViewGroup checkBoxContainer=(ViewGroup) row.findViewById(R.id.checkBoxContainer);

            myTitle.setText(titleArray[position]);
            myTitle.setTextSize(40);

            List<String> checkBoxTitles=details.get(titleArray[position]);

            for (String value : checkBoxTitles)
            {
                CheckBox checkbox=new CheckBox(context);
                checkbox.setText(value);
                checkbox.setTextSize(40);
                checkBoxContainer.addView(checkbox);
            }
            return row;

        }else {
            View row = inflater.inflate(R.layout.details_single_row_dropdown, parent, false);
            TextView myTitle=(TextView) row.findViewById(R.id.textView1);
            myTitle.setText(titleArray[position]);
            myTitle.setTextSize(40);
            List<String> spinnerTitles=details.get(titleArray[position]);
            Spinner spinner = (Spinner) findViewById(R.id.spinner_details);
            ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, spinnerTitles);
            spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(spinnerArrayAdapter);
            return row;

        }

    }

在这一行

spinner.setAdapter(spinnerArrayAdapter);
该应用程序崩溃了。

堆栈跟踪:

12-24 14:31:28.362: E/AndroidRuntime(32131): FATAL EXCEPTION: main
12-24 14:31:28.362: E/AndroidRuntime(32131): Process: com.tfl.techmanager, PID: 32131
12-24 14:31:28.362: E/AndroidRuntime(32131): java.lang.NullPointerException
12-24 14:31:28.362: E/AndroidRuntime(32131):    at      com.tfl.techmanager.DetailsActivity$DetailsAdapter.getView(DetailsActivity.java:174)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.widget.AbsListView.obtainView(AbsListView.java:2255)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.widget.ListView.onMeasure(ListView.java:1175)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.View.measure(View.java:16497) 
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.Choreographer.doFrame(Choreographer.java:544)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.os.Handler.handleCallback(Handler.java:733)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.os.Looper.loop(Looper.java:136)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at android.app.ActivityThread.main(ActivityThread.java:5001)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at java.lang.reflect.Method.invokeNative(Native Method)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at java.lang.reflect.Method.invoke(Method.java:515)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-24 14:31:28.362: E/AndroidRuntime(32131):    at dalvik.system.NativeStart.main(Native Method)

帮助......请

4 个答案:

答案 0 :(得分:1)

我认为问题出在这里

Spinner spinner = (Spinner) findViewById(R.id.spinner_details);

你忘记了排。

答案 1 :(得分:1)

改变这个:

Spinner spinner = (Spinner) findViewById(R.id.spinner_details);

用这个:

Spinner spinner = (Spinner) row.findViewById(R.id.spinner_details);

Godd luck

答案 2 :(得分:0)

更改此内容
Spinner spinner = (Spinner) findViewById(R.id.spinner_details);

Spinner spinner = (Spinner) row.findViewById(R.id.spinner_details);

答案 3 :(得分:0)

试试这个.......

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_layout_ip_addtional_users_access_right, null);
    }

    //Handle TextView and display string from your list
    TextView listItemText2 = (TextView)view.findViewById(R.id.list_item_string_property);
    listItemText2.setText(list.get(position));


    Spinner dropdown1 = (Spinner) view.findViewById(R.id.spinnerIP_UD_ACC);
    String[] items1 = new String[]{"Admin", "Read only", "Forbidden"};
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, items1);
    dropdown1.setAdapter(adapter1);

    return view;
}