所以,我有一个活动作为一些片段的容器。此活动有一个导航抽屉,可帮助用户在片段之间导航。
其中一个片段(在本例中称为HomeFragment)在加载时将从服务器获取一些数据,然后将它们放入HomeFragment内的ListView中。此过程在onViewCreated()中完成。
现在,问题是,当我完成活动时,HomeFragment的onViewCreated会以某种方式被调用。但是因为当片段从服务器获取数据时活动已经完成,它无法找到将数据放入的视图。这会导致应用崩溃。
我的MainActivity.java(包含片段):
public void setDrawerItem(MenuItem menuItem){
...
if (menuItem.getItemId() == R.id.logout) {
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
finish();
}
...
}
因此,当用户注销时,MainActivity将完成,LoginActivity将启动。
在我的HomeFragment中:
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView) view.findViewById(R.id.home_listView);
//fill aspiration_list with aspirations from database, then populate listview
new AspirationGetAll().execute();
}
AspirationGetAll类是一个ASyncTask,它将从数据库中获取数据,并填充listview。
logcat的:
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: FATAL EXCEPTION: main
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: Process: com.jefsolution.yourvote, PID: 14290
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at com.jefsolution.yourvote.Fragment.HomeFragment$AspirationGetAll.onPostExecute(HomeFragment.java:179)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at com.jefsolution.yourvote.Fragment.HomeFragment$AspirationGetAll.onPostExecute(HomeFragment.java:147)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at android.os.AsyncTask.finish(AsyncTask.java:632)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5260)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-19 01:49:05.408 14290-14290/com.jefsolution.yourvote E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-19 01:49:06.778 14290-14290/com.jefsolution.yourvote I/Process: Sending signal. PID: 14290 SIG: 9
当不点击抽屉中的注销按钮时,该应用程序正常工作。 HomeFragment中的第179行是:
AspirationAdapter adapter = new AspirationAdapter(getActivity().getApplicationContext(), aspiration_list);
第147行是AspirationGetAll类的声明。
这是处理导航抽屉项目单击的方法。是否有任何可能再次触发HomeFragment生命周期(因此在单击注销时调用onViewCreated())?
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the planet to show based on
// position
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.home_fragment:
fragmentClass = HomeFragment.class;
break;
case R.id.trending_fragment:
fragmentClass = TrendingFragment.class;
break;
case R.id.my_aspiration_fragment:
fragmentClass = MyAspirationFragment.class;
break;
default:
fragmentClass = HomeFragment.class;
}
if (menuItem.getItemId() == R.id.logout) {
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
finish();
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item, update the title, and close the drawer
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}
答案 0 :(得分:0)
尝试在创建视图时点击Web服务,即
onCreateView(LayoutInflater, ViewGroup, Bundle)
{
...
}
在onViewCreated
返回后立即调用 onCreateView(LayoutInflater, ViewGroup, Bundle)
,但在将任何已保存的状态恢复到视图之前。这使得子类有机会在知道自己的视图层次结构已完全创建后进行初始化。并且空指针异常的主要原因是适配器视图中Context的不可用性。您是否尝试过使用getActivity()
代替getActivity().getApplicationContext()
?