第一活动:
// static so that it survives orientation change.
private static int mSelectedItemPosition = -1; // -1 = Not selected
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mSelectedItemPosition != -1) {
getLoaderManager().initLoader(mSelectedItemPosition, null, MainActivity.this);
}
getActionBar().setListNavigationCallbacks(adapter, new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if(itemPosition == 0 || itemPosition ==1) {
mSelectedItemPosition = 0;
Bundle b = ...
getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
}
else if(itemPosition == 2) {
mSelectedItemPosition = itemPosition;
Bundle b = ...
getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
}
else {
mSelectedItemPosition = itemPosition;
Bundle b = ...
getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
}
...
}
从第一个活动我要去第二个活动。在第二个活动中,当我更改方向并通过单击系统后退按钮返回第一个活动时,我收到以下异常。如果我在不改变方向的情况下正常返回,我不会有任何异常。
**Exception Details:**
01-31 02:59:20.810: E/AndroidRuntime(7165): FATAL EXCEPTION: main
01-31 02:59:20.810: E/AndroidRuntime(7165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.schoolinsites.mcpss/com.schoolinsites.mcpss.MainActivity}: java.lang.NullPointerException
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1996)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2023)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3393)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.ActivityThread.access$700(ActivityThread.java:127)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1178)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.os.Looper.loop(Looper.java:137)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.ActivityThread.main(ActivityThread.java:4503)
01-31 02:59:20.810: E/AndroidRuntime(7165): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 02:59:20.810: E/AndroidRuntime(7165): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 02:59:20.810: E/AndroidRuntime(7165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
01-31 02:59:20.810: E/AndroidRuntime(7165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
01-31 02:59:20.810: E/AndroidRuntime(7165): at dalvik.system.NativeStart.main(Native Method)
01-31 02:59:20.810: E/AndroidRuntime(7165): Caused by: java.lang.NullPointerException
01-31 02:59:20.810: E/AndroidRuntime(7165): at com.schoolinsites.DataAccessLayer.GCCLoader.<init>(GCCLoader.java:22)
01-31 02:59:20.810: E/AndroidRuntime(7165): at com.schoolinsites.mcpss.MainActivity.onCreateLoader(MainActivity.java:352)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.LoaderManagerImpl.createLoader(LoaderManager.java:500)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:509)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.LoaderManagerImpl.initLoader(LoaderManager.java:563)
01-31 02:59:20.810: E/AndroidRuntime(7165): at com.schoolinsites.mcpss.MainActivity.CallInitLoader(MainActivity.java:371)
01-31 02:59:20.810: E/AndroidRuntime(7165): at com.schoolinsites.mcpss.MainActivity.onCreate(MainActivity.java:153)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.Activity.performCreate(Activity.java:4516)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
01-31 02:59:20.810: E/AndroidRuntime(7165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
01-31 02:59:20.810: E/AndroidRuntime(7165): ... 12 more
从异常详细信息中,我知道这Asynctaskloader
是在给予NullPointerException
。
谁能告诉我如何解决这个问题?我无法理解为什么当我通过改变方向回来时会发生这种情况。
数据访问层:
public class GCCLoader extends AsyncTaskLoader<List<Item>> {
private String Url, XmlRoles;
private int selection;
private List<Item> mData;
public GCCLoader(Context context, Bundle extras) {
super(context);
this.Url = extras.getString(Constatnts.Url, null);
this.selection =extras.getInt(Constatnts.selection,0);
this.XmlRoles = extras.getString(Constatnts.xmlroles,"");
}
@Override
public List<Item> loadInBackground() {
// This method is called on a background thread and should generate a
// new set of data to be delivered back to the client.
List<Item> data = new ArrayList<Item>();
VideoDal dal = new VideoDal();
data.addAll(dal.GetGCC(selection, dal.GetDocument(Url, XmlRoles, null)));
return data;
}
/********************************************************/
/** (2) Deliver the results to the registered listener **/
/********************************************************/
@Override
public void deliverResult(List<Item> data) {
if (isReset()) {
// The Loader has been reset; ignore the result and invalidate the data.
releaseResources(data);
return;
}
// Hold a reference to the old data so it doesn't get garbage collected.
// We must protect it until the new data has been delivered.
List<Item> oldData = mData;
mData = data;
if (isStarted()) {
// If the Loader is in a started state, deliver the results to the
// client. The superclass method does this for us.
super.deliverResult(data);
}
// Invalidate the old data as we don't need it any more.
if (oldData != null && oldData != data) {
releaseResources(oldData);
}
}
/*********************************************************/
/** (3) Implement the Loader’s state-dependent behavior **/
/*********************************************************/
@Override
protected void onStartLoading() {
if (mData != null) {
// Deliver any previously loaded data immediately.
deliverResult(mData);
}
if (takeContentChanged() || mData == null) {
// When the observer detects a change, it should call onContentChanged()
// on the Loader, which will cause the next call to takeContentChanged()
// to return true. If this is ever the case (or if the current data is
// null), we force a new load.
forceLoad();
}
}
@Override
protected void onStopLoading() {
// The Loader is in a stopped state, so we should attempt to cancel the
// current load (if there is one).
cancelLoad();
// Note that we leave the observer as is. Loaders in a stopped state
// should still monitor the data source for changes so that the Loader
// will know to force a new load if it is ever started again.
}
@Override
protected void onReset() {
// Ensure the loader has been stopped.
onStopLoading();
// At this point we can release the resources associated with 'mData'.
if (mData != null) {
releaseResources(mData);
mData = null;
}
}
@Override
public void onCanceled(List<Item> data) {
// Attempt to cancel the current asynchronous load.
super.onCanceled(data);
// The load has been canceled, so we should release the resources
// associated with 'data'.
releaseResources(data);
}
private void releaseResources(List<Item> data) {
// For a simple List, there is nothing to do. For something like a Cursor, we
// would close it in this method. All resources associated with the Loader
// should be released here.
}
}