每当用户使用蜂窝网退出我的应用程序(MainActivity)时,我都会收到以下错误:
06-12 02:30:42.612: E/AndroidRuntime(322): FATAL EXCEPTION: main
06-12 02:30:42.612: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to destroy activity {com.maddogs.mymoney/com.maddogs.mymoney.MainActivity}: java.lang.NullPointerException
06-12 02:30:42.612: E/AndroidRuntime(322): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.app.ActivityThread.access$2900(ActivityThread.java:125)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-12 02:30:42.612: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 02:30:42.612: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:521)
06-12 02:30:42.612: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-12 02:30:42.612: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-12 02:30:42.612: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method)
06-12 02:30:42.612: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException
06-12 02:30:42.612: E/AndroidRuntime(322): at com.maddogs.mymoney.adapters.PeopleCursorAdapter.getItemId(PeopleCursorAdapter.java:69)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.widget.AbsListView.onSaveInstanceState(AbsListView.java:885)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.widget.ListView.onSaveInstanceState(ListView.java:3625)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.widget.AdapterView.access$100(AdapterView.java:42)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:800)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:43)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:54)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.support.v4.widget.CursorAdapter.swapCursor(CursorAdapter.java:352)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.support.v4.widget.CursorAdapter.changeCursor(CursorAdapter.java:315)
06-12 02:30:42.612: E/AndroidRuntime(322): at com.maddogs.mymoney.MainActivity.onLoaderReset(MainActivity.java:311)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.support.v4.app.LoaderManagerImpl$LoaderInfo.destroy(LoaderManager.java:339)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.support.v4.app.LoaderManagerImpl.doDestroy(LoaderManager.java:776)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:337)
06-12 02:30:42.612: E/AndroidRuntime(322): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
06-12 02:30:42.612: E/AndroidRuntime(322): ... 11 more
我无法理解为什么会在Honeycomb上发生这种情况,但在以后的版本上却没有(我在移动S3上使用android 4.2测试过)。
我的代码:
PeopleCursorAdapter:
package com.maddogs.mymoney.adapters;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.maddogs.mymoney.R;
import com.maddogs.mymoney.provider.MyMoneyContract.Users;
import com.maddogs.mymoney.utils.Utils;
import com.squareup.picasso.Picasso;
public class PeopleCursorAdapter extends CursorAdapter {
Context context;
LayoutInflater inflater;
public PeopleCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
// String id =
// arg2.getString(arg2.getColumnIndexOrThrow(Users.USER_ID));
String name = arg2.getString(arg2
.getColumnIndexOrThrow(Users.USER_NAME));
// String email = arg2.getString(arg2
// .getColumnIndexOrThrow(Users.USER_EMAIL));
// String profilePic = arg2.getString(arg2
// .getColumnIndexOrThrow(Users.USER_PROFILEPIC));
Double value = arg2.getDouble(arg2
.getColumnIndexOrThrow(Users.USER_BALANCE));
TextView nameTextView = (TextView) arg0.findViewById(R.id.peopleName);
nameTextView.setText(name);
TextView valueTextView = (TextView) arg0
.findViewById(R.id.peopleDebitsBalance);
if (value >= 0.00)
valueTextView.setTextColor(Color.parseColor("#0000ff"));
else
valueTextView.setTextColor(Color.RED);
valueTextView.setText(Utils.convertCurrency(arg1, value));
ImageView profilePicImageView = (ImageView) arg0
.findViewById(R.id.peopleProfilePicture);
Picasso.with(context).load(R.drawable.rata_logo)
.into(profilePicImageView);
}
@Override
public long getItemId(int position) {
Cursor c = getCursor();
if (c.moveToPosition(position)) {
return Long.valueOf(c.getInt(c.getColumnIndexOrThrow(Users._ID)));
} else
return -1;
}
public String getPersonName(int position) {
Cursor c = getCursor();
if (c.moveToPosition(position)) {
return c.getString(c.getColumnIndexOrThrow(Users.USER_NAME));
} else
return null;
}
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
return inflater.inflate(R.layout.adapter_people, arg2, false);
}
}
MainActivity(一些片段):
public class MainActivity extends ActionBarListActivity implements
OnItemClickListener, OnItemLongClickListener, LoaderCallbacks<Cursor> {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(
getSupportLoaderManager().initLoader(0, null, this);
adapter = new PeopleCursorAdapter(this, null, 0);
this.setListAdapter(adapter);
...
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
CursorLoader loader = new CursorLoader(this, UsersListView.CONTENT_URI,
new String[] { Users.USER_BALANCE, Users.USER_NAME,
Users.USER_ID, Users._ID }, null, null, null);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
this.adapter.changeCursor(arg1);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
this.adapter.changeCursor(null);
}
由于
答案 0 :(得分:0)
在onDestroy中,您需要检查适配器是否为空并删除适配器的基础光标
示例:强>
protected void onDestroy() {
if( adapter != null ) {
adapter.changeCursor( null );
adapter = null;
}
super.onDestroy();
}
检查
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
if(this.adapter != null)
this.adapter.changeCursor(arg1);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
if(this.adapter != null)
this.adapter.changeCursor(null);
}
答案 1 :(得分:0)
不想解决这个问题:
@Override
public long getItemId(int position) {
Cursor c = getCursor();
if (c == null)
return -1;
if (c.moveToPosition(position)) {
return Long.valueOf(c.getInt(c.getColumnIndexOrThrow(Users._ID)));
} else
return -1;
}
public String getPersonName(int position) {
Cursor c = getCursor();
if (c == null)
return null;
if (c.moveToPosition(position)) {
return c.getString(c.getColumnIndexOrThrow(Users.USER_NAME));
} else
return null;
}