设备轮换时ViewPager和LoaderManager错误

时间:2012-07-13 08:22:19

标签: android android-viewpager android-loadermanager

我在那里使用一个带有3个片段的ViewPager。其他使用LoaderManager加载每个片段数据。我实现了自己的加载器,但组合分页器和加载器在NullPointerException中运行,如果我旋转设备并且实际选择了加载器选项卡。这是我的logcat。

  

显示java.lang.NullPointerException   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:960)   android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:768)   android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1104)   android.support.v4.app.LoaderManagerImpl $ LoaderInfo.onLoadComplete(LoaderManager.java:410)   android.support.v4.content.Loader.deliverResult(Loader.java:103)   com.android.droidfridge.loader.FridgeLoader.deliverResult(FridgeLoader.java:144)   com.android.droidfridge.loader.FridgeLoader.deliverResult(FridgeLoader.java:1)   android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:221)   android.support.v4.content.AsyncTaskLoader $ LoadTask.onPostExecute(AsyncTaskLoader.java:61)   android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)   android.support.v4.content.ModernAsyncTask.access $ 500(ModernAsyncTask.java:47)   android.support.v4.content.ModernAsyncTask $ InternalHandler.handleMessage(ModernAsyncTask.java:474)

这是我的完整加载程序代码:

package com.android.droidfridge.loader;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;

import com.android.droidfridge.adapters.FridgeEntryItem;
import com.android.droidfridge.adapters.Item;
import com.android.droidfridge.adapters.SectionItem;
import com.android.droidfridge.database.DatabaseHelper;
import com.android.droidfridge.database.Items;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;

public class FridgeLoader extends AsyncTaskLoader<List<Item>>
{
    private final static String TAG = "FridgeLoader";

    private DatabaseHelper db = null;
    private List<Item> item;

    private ItemComparator option;
    private boolean ascending;

    public FridgeLoader( Context context, DatabaseHelper db, ItemComparator option, boolean ascending )
    {
        super( context );
        this.db = db;
        this.option = option;
        this.ascending = ascending;
    }

    @Override
    public List<Item> loadInBackground()
    {
        Log.i( TAG, "Started loadInBackground." );

        ArrayList<Item> result = new ArrayList<Item>();
        TreeSet<String> sections = new TreeSet<String>();

        try
        {
            // search for all items
            Dao<Items, Integer> dao = db.getItemsDao();
            QueryBuilder<Items, Integer> builder = dao.queryBuilder();
            builder.where().gt( Items.ACTUAL_AMOUNT_COLUMN, 0 );
            List<Items> data = dao.query( builder.prepare() );

            // sort items list
            data = sortData( data );

            // create list view items
            for( Items item : data )
            {
                // add Category
                if( !sections.contains( item.getCategory().getName() ) )
                {
                    sections.add( item.getCategory().getName() );
                    result.add( new SectionItem( item.getCategory() ) );
                }

                // add Item
                result.add( new FridgeEntryItem( item ) );
            }
        }
        catch( SQLException e )
        {
            Log.e( TAG, "loadInBackground: " + e.getMessage() );
        }

        return result;
    }

    private List<Items> sortData( List<Items> data )
    {
                //...
        return sortedResult;
    }

    @Override
    public void deliverResult( List<Item> data )
    {
        if( isReset() )
        {
            if( data != null )
            {
                onReleaseResources( data );
            }
        }

        List<Item> oldData = data;
        item = data;

        if( isStarted() )
        {
            super.deliverResult( data );
        }

        if( oldData != null )
        {
            onReleaseResources( oldData );
        }
    }

    @Override
    protected void onStartLoading()
    {
        Log.d( TAG, "onStartLoading()" );

        if( item != null )
        {
            deliverResult( item );
        }

        if( takeContentChanged() || item == null )
        {
            forceLoad();
        }
    }

    @Override
    protected void onStopLoading()
    {
        Log.d( TAG, "onStopLoading()" );

        cancelLoad();
    }

    @Override
    public void onCanceled( List<Item> data )
    {
        Log.d( TAG, "onCanceled()" );
        super.onCanceled( data );
        onReleaseResources( data );
    }

    @Override
    protected void onReset()
    {
        Log.d( TAG, "onReset()" );
        super.onReset();

        onStopLoading();

        if( item != null )
        {
            onReleaseResources( item );
            item = null;
        }
    }

    private void onReleaseResources( List<Item> data )
    {
    }
}

1 个答案:

答案 0 :(得分:2)

我找到了解决问题的方法。它位于片段交易中。重要的是这个:

setRetainInstance( true );

这一行来自我的片段,它使用上面的这个加载器。这是他的建设者。