长时间不活动后,在swapcursor上获取nullpointer

时间:2013-03-21 20:26:07

标签: android android-fragments android-loadermanager

我的加载器,片段和内容提供商工作正常。我的所有列表都可以更新,但是在长时间不活动(主要活动暂停)后,如果我重新激活应用程序,我会在swapsursor调用上获得空指针

public class ListeFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private ListItemSelectListener listeSelectListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listeSelectListener = (ListItemSelectListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
            + " doit implementer ListItemSelectListener");
        }
    }

    // public void onSaveInstanceState (Bundle outState) {
    // super.onSaveInstanceState(outState);
    // int scroll = this.getSelectedItemPosition();
    // outState.putInt("POS", scroll); }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.e ("Resto", "pos="+position+" Rowid= "+id);
        listeSelectListener.onItemSelected(position, id);      
    }

    private static final int RESTO_RECENT_LOADER = 0x01;
    private static final int RESTO_ALPHA_LOADER = 0x02;
    private static final int RESTO_HIGH_LOADER = 0x03;
    private static final int RESTO_SEARCH_LOADER = 0x04;
    // private static final String AUTHORITY = "ca.usimage.resto.RestoProvider";
    // private static final String RESTOS_BASE_PATH = "restos";

    private SimpleCursorAdapter adapter;

    public void afficheList(int loader_id, String query) {
        Log.e ("Resto", "loader_id= "+loader_id);
        Bundle mBundle = new Bundle();
        mBundle.putString("search_query", query);
        getLoaderManager().restartLoader(loader_id, mBundle, this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
        if (savedInstanceState == null) {
            String[] uiBindFrom = { RestoDatabase.COL_ETAB, RestoDatabase.COL_MONTANT };
            int[] uiBindTo = { R.id.TextView01, R.id.Montant };
            // default loader on startup is RECENT_LOADER
            LoaderManager lm = getLoaderManager();
            if (lm.getLoader(RESTO_RECENT_LOADER) != null) {
                lm.initLoader(RESTO_RECENT_LOADER, null, this);
            }

            // if (adapter == null) {
            adapter = new SimpleCursorAdapter(
                getActivity().getApplicationContext(), R.layout.row,
                null, uiBindFrom, uiBindTo,
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            setListAdapter(adapter);
            // }
            // this.setSelection(savedInstanceState.getInt("POS"));
        }
    }

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = { RestoDatabase.ID, RestoDatabase.COL_ETAB,  RestoDatabase.COL_MONTANT };
        switch (id){
        case RESTO_RECENT_LOADER:
            return new CursorLoader(getActivity(),
                RestoProvider.CONTENT_URI, projection, null, null, "date_infraction DESC");

        case RESTO_ALPHA_LOADER:
            return new CursorLoader(getActivity(),
                RestoProvider.CONTENT_URI_GROUPBY, projection, null, null,"etablissement ASC");

        case RESTO_HIGH_LOADER:
            return new CursorLoader(getActivity(),
                RestoProvider.CONTENT_URI, projection, null, null, "montant DESC");

        case RESTO_SEARCH_LOADER:
            return new CursorLoader(getActivity(),
                RestoProvider.CONTENT_URI, projection, "etablissement like \"%" + args.getString("search_query") + "%\"", null, "etablissement ASC");

        default: return null;
        }
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        adapter.swapCursor(cursor);
        // position cursor at top of list
        // this.setSelection(0);
    }

    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }

似乎操作栏存在问题,因为我正在尝试使用默认选中的选项卡,该选项卡在启动时调用加载程序。

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  ;
        setContentView(R.layout.liste);
        final ActionBar ab = getActionBar();

        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ab.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

        if (savedInstanceState != null){
            tab_pos = savedInstanceState.getInt("tabState");               
            ab.addTab(ab.newTab().setText(R.string.tab_recente).setTabListener(this),0,false);
            ab.addTab(ab.newTab().setText(R.string.tab_alpha).setTabListener(this),1,false);
            ab.addTab(ab.newTab().setText(R.string.tab_fortes).setTabListener(this),2,false);
            ab.setSelectedNavigationItem(tab_pos);       
        } else {       
            ab.addTab(ab.newTab().setText(R.string.tab_recente).setTabListener(this),0,true);
            ab.addTab(ab.newTab().setText(R.string.tab_alpha).setTabListener(this),1,false);
            ab.addTab(ab.newTab().setText(R.string.tab_fortes).setTabListener(this),2,false);
        }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // position cursor at top of list if user retaps a tab
        ListeFragment listeFrg =  (ListeFragment)getFragmentManager().findFragmentById(R.id.listeFragment);
        listeFrg.setSelection(0);
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

        ListeFragment listeFrg = (ListeFragment)getFragmentManager().findFragmentById(R.id.listeFragment);

        int position = tab.getPosition();
        Log.e ("resto", "pos= "+position);
        switch (position) {
        case 0:        
            listeFrg.afficheList(RESTO_RECENT_LOADER, null);
            break;
        case 1:
            listeFrg.afficheList(RESTO_ALPHA_LOADER, null);
            break;
        case 2:
            listeFrg.afficheList(RESTO_HIGH_LOADER, null);
            break;
        }           
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub    
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tabState", getActionBar().getSelectedTab().getPosition());
    }

1 个答案:

答案 0 :(得分:2)

您的问题最有可能发生在onActivityCreated

长时间不活动后,操作系统会破坏您的活动。 当您尝试返回活动时,会重新创建。 调用onCreated活动后,调用片段的onActivityCreated, 但是只有adapter为空时才有条件创建savedInstanceState。 如果它不为null,则不创建adapter,这会在以后访问时导致空指针异常。