不能在spinner.setAdapter中使用主类扩展ListFragment

时间:2015-01-12 14:17:15

标签: java android android-listfragment

当我使用此代码时,我的应用程序崩溃,我无法编辑它。

public class HomeFragment extends ListFragment implements OnItemClickListener{
public HomeFragment(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    .
    .
    addItemsOnSpinner1();
    .
    .
    return rootView;
}
public void addItemsOnSpinner1() {
    spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1);
    List<String> list = new ArrayList<String>();
    db = new DBAdapter(this.getActivity());
    db.open();
    Cursor d = db.fetchAllmahallename();
    if(d.moveToFirst()){
        do{
            list.add(d.getString(0));
        }
        while(d.moveToNext());
    }
    d.close();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity().getApplicationC  ontext(), android.R.layout.simple_spinner_item,list);
    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layo  ut.simple_spinner_dropdown_item);
    Log.i("MyLog","here1");
    // attaching data adapter to spinner
    spinner1.setAdapter(dataAdapter);
    Log.i("MyLog","here2");
}

}

应用程序通过了Log.i(“MyLog”,“here1”);但在行中sprin1.setAdapter(dataAdapter); 我使用上面的代码,我的主类扩展ListActivity并正常工作,但当我强制使用扩展ListFragment我有这个问题。这是错误代码:

01-12 17:17:29.197: I/MyLog(6672): here1
01-12 17:17:29.207: D/AndroidRuntime(6672): Shutting down VM
01-12 17:17:29.207: W/dalvikvm(6672): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-12 17:17:29.217: E/AndroidRuntime(6672): FATAL EXCEPTION: main
01-12 17:17:29.217: E/AndroidRuntime(6672): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.NullPointerException
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.os.Looper.loop(Looper.java:137)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at java.lang.reflect.Method.invoke(Method.java:511)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at dalvik.system.NativeStart.main(Native Method)
01-12 17:17:29.217: E/AndroidRuntime(6672): Caused by: java.lang.NullPointerException
01-12 17:17:29.217: E/AndroidRuntime(6672):     at info.androidhive.slidingmenu.HomeFragment.addItemsOnSpinner1(HomeFragment.java:240)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at info.androidhive.slidingmenu.HomeFragment.onCreateView(HomeFragment.java:129)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.BackStackRecord.run(BackStackRecord.java:622)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.Activity.performStart(Activity.java:4474)
01-12 17:17:29.217: E/AndroidRuntime(6672):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
01-12 17:17:29.217: E/AndroidRuntime(6672):     ... 11 more

1 个答案:

答案 0 :(得分:1)

替换此行

spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1);

通过这一行

spinner1 = (Spinner) rootView .findViewById(R.id.spinner1);

希望它有效......