在android片段中使用键值对填充微调器

时间:2014-05-20 08:13:30

标签: java android android-fragments spinner key-value

我正在尝试在片段中创建一个带键值对的微调器。我得到这个logcat输出:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ica.icadashboard/com.ica.icadashboard.HomeActivity}: java.lang.NullPointerException
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 android.app.ActivityThread.access$600(ActivityThread.java:141)
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 android.os.Handler.dispatchMessage(Handler.java:99)
 android.os.Looper.loop(Looper.java:137)
 android.app.ActivityThread.main(ActivityThread.java:5041)
 java.lang.reflect.Method.invokeNative(Native Method)
 java.lang.reflect.Method.invoke(Method.java:511)
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 dalvik.system.NativeStart.main(Native Method)
05-20 08:06:36.247: E/AndroidRuntime(2746): Caused by: java.lang.NullPointerException
 android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
 android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
 com.ica.placement.PlacementFragment.setSpinnerContent(PlacementFragment.java:44)
 com.ica.placement.PlacementFragment.onCreateView(PlacementFragment.java:29)
 android.app.Fragment.performCreateView(Fragment.java:1695)
 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
 android.app.BackStackRecord.run(BackStackRecord.java:682)
 android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
 android.app.Activity.performStart(Activity.java:5113)
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
05-20 08:06:36.247: E/AndroidRuntime(2746):     ... 11 more

片段类

public class PlacementFragment extends Fragment {

    Spinner adm_spinner;
    Activity activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_placement, container, false);
          // Now use the above view to populate the spinner.
          setSpinnerContent( view );
                return container;

    }

    private void setSpinnerContent( View view )
    {
        adm_spinner = (Spinner) view.findViewById( R.id.adm_spinner );

        final MyData items[] = new MyData[4];

        for (int i = 0; i <= 3; i++) {
            items[i] = new MyData("value " + i, "" + i);
        }

        ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(activity,
                android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adm_spinner.setAdapter(adapter);
     // spinner.setAdapter( adapter );
    }



    class MyData {
        public MyData(String spinnerText, String value) {
            this.spinnerText = spinnerText;
            this.value = value;
        }

        public String getSpinnerText() {
            return spinnerText;
        }

        public String getValue() {
            return value;
        }

        public String toString() {
            return spinnerText;
        }

        String spinnerText;
        String value;
    }
}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Placement Analysis"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:paddingTop="5dp" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="1" >

                        <Spinner
                            android:id="@+id/tp_spinner"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight=".5" />

                        <Spinner
                            android:id="@+id/adm_spinner"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight=".5" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="1" >

                        <Spinner
                            android:id="@+id/centre_spinner"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight=".5" />

                        <Spinner
                            android:id="@+id/vertical_spinner"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight=".5" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="1" >

                        <Button
                            android:id="@+id/calendar_button"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Calendar" />

                        <Button
                            android:id="@+id/process_button"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Process" />
                    </LinearLayout>
                </TableRow>
            </TableLayout>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

有什么问题?我哪里错了?我应该如何填充微调器?

3 个答案:

答案 0 :(得分:3)

你应该

 return view;

而不是

 return container;
<{1}} onCreateView(.....)

中的

也改变了这个

PlacementFragment

 ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(activity,
            android.R.layout.simple_spinner_item, items);

答案 1 :(得分:0)

  

引起:java.lang.NullPointerException   android.widget.ArrayAdapter.init(ArrayAdapter.java:310)

除了您需要更改为

之外的其他答案
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(getActivity(),
            android.R.layout.simple_spinner_item, items);

Activity activity;活动未初始化。

答案 2 :(得分:0)

试试这个..

您尚未初始化活动 activity将其初始化为

activity = getActivity();