繁体   English   中英

父活动的finish()会调用片段的onViewCreated()吗?

[英]Fragment's onViewCreated() get called when parent activity finish()?

因此,我有一个活动,可以作为一些片段的容器。 该活动具有导航抽屉,以帮助用户在片段之间导航。

片段之一(在本例中称为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,它将从数据库中获取数据,并填充列表视图。

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();
    }

创建视图时尝试点击Web服务,例如

onCreateView(LayoutInflater, ViewGroup, Bundle)
{
    ...
}

onViewCreated后立即调用onCreateView(LayoutInflater, ViewGroup, Bundle)又回来了,但是任何保存的状态已经到视图中恢复之前。 一旦子类知道其视图层次结构已完全创建,这将给子类一个初始化自身的机会。 空指针异常的主要原因是适配器视图中的上下文不可用。 您是否尝试过使用getActivity()而不是getActivity().getApplicationContext()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM