当我使用此代码时,我的应用崩溃了,无法对其进行编辑。
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”);但是在spinner1.setAdapter(dataAdapter)行中崩溃了;
我使用上面的代码在我的主类扩展ListActivity并正常工作的地方,但是当我强制使用extends 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
最佳答案
替换这条线
spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1);
通过这条线
spinner1 = (Spinner) rootView .findViewById(R.id.spinner1);
希望它能...