そこに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 )
{
}
}