0
我使用listfragment並嘗試更改它的內容。這裏是我的活動代碼:更改listfragment內容
public class MainActivity extends Activity {
public static Context appContext;
public ToFragment ToFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionbar = getActionBar();
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab FromTab = actionbar.newTab().setText("FromFragment");
ActionBar.Tab ToTab = actionbar.newTab().setText("ToFragment");
FromFragment FromFragment = new FromFragment();
ToFragment ToFragment = new ToFragment();
ToTab.setTabListener(new MyTabsListener(ToFragment));
FromTab.setTabListener(new MyTabsListener(FromFragment));
// ListView tolistview = ToFragment.getListView();
// ListView fromlistview = FromFragment.getListView();
actionbar.addTab(ToTab);
actionbar.addTab(FromTab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
String[] countries = new String[] {
"Russia",
"USA",
};
ToFragment.changeContent(countries);
break;
case R.id.action_settings:
break;
default:
break;
}
return true;
}
}
在這裏,我初始化2個listfragments,ToFragment和FromFragment。然後在這裏(onCreateOptionsMenu)我嘗試通過我的函數changeContent更改ToFragment的內容。這裏是片段的代碼:我tryed使功能changeContent工作就像函數onCreateView
public class ToFragment extends ListFragment {
String[] countries = new String[] {
"India",
"Pakistan",
};
private LayoutInflater inflater;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, countries);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
public void changeContent(String[] array) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, array);
setListAdapter(adapter);
}
,如你所見。但!我的問題是,它在這裏拋出nullpointerexception,我真的很困惑,我不明白什麼是問題!你能告訴我什麼是錯的嗎?或者你可以建議更好的方式來改變listfragment內容?
這是錯誤的軌道:
04-08 22:48:33.292: I/System.out(8817): adapter == null
04-08 22:48:33.292: W/dalvikvm(8817): threadid=1: thread exiting with uncaught exception (group=0x414572d0)
04-08 22:48:33.297: E/AndroidRuntime(8817): FATAL EXCEPTION: main
04-08 22:48:33.297: E/AndroidRuntime(8817): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.rkarasev.miptrain/ru.rkarasev.miptrain.MainActivity}: java.lang.NullPointerException
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.app.ActivityThread.access$600(ActivityThread.java:151)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.os.Looper.loop(Looper.java:155)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.app.ActivityThread.main(ActivityThread.java:5493)
04-08 22:48:33.297: E/AndroidRuntime(8817): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 22:48:33.297: E/AndroidRuntime(8817): at java.lang.reflect.Method.invoke(Method.java:511)
04-08 22:48:33.297: E/AndroidRuntime(8817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
04-08 22:48:33.297: E/AndroidRuntime(8817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
04-08 22:48:33.297: E/AndroidRuntime(8817): at dalvik.system.NativeStart.main(Native Method)
04-08 22:48:33.297: E/AndroidRuntime(8817): Caused by: java.lang.NullPointerException
04-08 22:48:33.297: E/AndroidRuntime(8817): at ru.rkarasev.miptrain.ToFragment.changeContent(ToFragment.java:34)
04-08 22:48:33.297: E/AndroidRuntime(8817): at ru.rkarasev.miptrain.MainActivity.onCreate(MainActivity.java:49)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.app.Activity.performCreate(Activity.java:5066)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
04-08 22:48:33.297: E/AndroidRuntime(8817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
04-08 22:48:33.297: E/AndroidRuntime(8817): ... 11 more
04-08 22:48:35.382: D/Process(8817): killProcess, pid=8817
04-08 22:48:35.387: D/Process(8817): dalvik.system.VMStack.getThreadStackTrace(Native Method)
04-08 22:48:35.387: D/Process(8817): java.lang.Thread.getStackTrace(Thread.java:599)
04-08 22:48:35.387: D/Process(8817): android.os.Process.killProcess(Process.java:944)
04-08 22:48:35.392: D/Process(8817): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
04-08 22:48:35.392: D/Process(8817): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
04-08 22:48:35.392: D/Process(8817): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
04-08 22:48:35.392: D/Process(8817): dalvik.system.NativeStart.main(Native Method)
它仍然拋出nullpointerexception。也許在這裏? – user1933770 2013-04-08 05:04:20
adapter.clear(); – user1933770 2013-04-08 05:04:36
是的,我確定那個適配器== null。如何得到它? – user1933770 2013-04-08 05:06:21