在片段中应用Spinner set adapter会导致NullPointerException

时间:2014-05-10 15:38:11

标签: android android-fragments android-listview android-spinner

我在片段中设置适配器到spinner有一些问题,它会导致NullPointerException 我不知道发生了什么,请帮助谢谢

public class GradeFragment extends ListFragment {

    private static final String[] LIST = { "102-1", "102-2","103-1","103-2" };

    List<String> list = new ArrayList<String>();
    private Spinner spinner;

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

        View view = inflater.inflate(R.layout.listview, container, false);
        spinner = (Spinner) view.findViewById(R.id.spinner);
        ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(
                getActivity().getApplicationContext(),
                android.R.layout.simple_spinner_item, list);
        arrayadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayadapter);
        spinner.setPrompt("select");
        spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
        return view;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    class SpinnerSelectedListener implements OnItemSelectedListener {    

        public void onItemSelected(AdapterView<?> parents, View v, int position, long arg3) {
            String str = parents.getItemAtPosition(position).toString();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
}

喷丝

<Spinner
  android:id="@+id/spinner"
  android:layout_width="wrap_content"
  android:layout_height="40dip"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true" />

日志:

05-10 23:21:16.316: E/AndroidRuntime(2436): FATAL EXCEPTION: main 
05-10 23:21:16.316: E/AndroidRuntime(2436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grade/com.example.grade.MainActivity}: android.view.InflateException: Binary XML file line #63: Error inflating class fragment 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.os.Handler.dispatchMessage(Handler.java:99) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.os.Looper.loop(Looper.java:137) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.ActivityThread.main(ActivityThread.java:5041) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at java.lang.reflect.Method.invokeNative(Native Method) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at java.lang.reflect.Method.invoke(Method.java:511) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at dalvik.system.NativeStart.main(Native Method) 
05-10 23:21:16.316: E/AndroidRuntime(2436): Caused by: android.view.InflateException: Binary XML file line #63: Error inflating class fragment 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at com.example.grade.InFragment.onCreateView(InFragment.java:14) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.Fragment.performCreateView(Fragment.java:1695) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.Activity.onCreateView(Activity.java:4717) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.Activity.setContentView(Activity.java:1881) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at com.example.grade.MainActivity.onCreate(MainActivity.java:12) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.Activity.performCreate(Activity.java:5104) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-10 23:21:16.316: E/AndroidRuntime(2436):     ... 11 more 
05-10 23:21:16.316: E/AndroidRuntime(2436): Caused by: java.lang.NullPointerException 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at com.example.grade.GradeFragment.onCreateView(GradeFragment.java:77)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.Fragment.performCreateView(Fragment.java:1695) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.app.Activity.onCreateView(Activity.java:4717) 
05-10 23:21:16.316: E/AndroidRuntime(2436):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-10 23:21:16.316: E/AndroidRuntime(2436):     ... 31 more

2 个答案:

答案 0 :(得分:2)

检查&#34; getActivity()上的getActivity()是否为null.getApplicationContext()&#34;在onCreateView()中,可以在Activity绑定到Fragment之前调用它,特别是当你在XML中定义Fragment时。

所以移动&#34;适配器&#34;到onActivityCreated()。

答案 1 :(得分:0)

首先,由于您转贴了ListFragment,因此您无需为视图充气,因此请删除该行

View view = inflater.inflate(R.layout.listview, container, false);

注意:如果您确实需要处理程序到列表视图,那么您只需执行

View view = findViewById(R.id.listview);

但是这个视图中没有微调器,所以它在这里不会有用。

您需要将微调器添加到此“FragmentList”视图中,一种方法是对其进行充气

View view = inflater.inflate(R.layout.MY_SPINNER_LAYOUT, container, this);