我使用parcelable获取onListItemClick的Uncaught异常,我已经用尽所有可能的搜索答案。该应用程序是一个观鸟应用程序,它使用标准列表视图和一个链接到详细活动的自定义适配器。鸟类及其细节使用SQLite在本地存储。任何帮助非常感谢..
只是添加..这是我的第一个应用程序和第一篇文章,所以要善待..
这是我的onListItemClick方法:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Bird bird = birds.get(position);
Intent intent = new Intent(this, Profile.class);
intent.putExtra(".model.Bird", bird);
startActivity(intent);
}
这是我的个人资料类:
public class Profile extends Activity{
Bird bird;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
Bundle b = getIntent().getExtras();
bird = b.getParcelable(".model.bird");
refreshDisplay();
}
这是我的模特(没有镶嵌和吸气):
public Bird() {
super();
}
public Bird(Parcel in) {
//Log.i(ReferenceGuide.LOGTAG, "Parcel constructor");
id = in.readLong();
name = in.readString();
latin_name = in.readString();
description = in.readString();
habitat = in.readString();
diet = in.readString();
breeding = in.readString();
wintering_habits = in.readString();
where_to_see = in.readString();
conservation = in.readString();
image = in.readString();
primary_colour = in.readInt();
secondary_colour = in.readInt();
crown_colour = in.readInt();
bill_length = in.readInt();
bill_colour = in.readInt();
tail_shape = in.readInt();
created_at = in.readString();
upated_at = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//Log.i(ReferenceGuide.LOGTAG, "writeToParcel");
dest.writeLong(id);
dest.writeString(name);
dest.writeString(latin_name);
dest.writeString(description);
dest.writeString(habitat);
dest.writeString(diet);
dest.writeString(breeding);
dest.writeString(wintering_habits);
dest.writeString(where_to_see);
dest.writeString(conservation);
dest.writeString(image);
dest.writeInt(primary_colour);
dest.writeInt(secondary_colour);
dest.writeInt(crown_colour);
dest.writeInt(bill_length);
dest.writeInt(bill_colour);
dest.writeInt(tail_shape);
dest.writeString(created_at);
dest.writeString(upated_at);
}
public static final Parcelable.Creator<Bird> CREATOR =
new Parcelable.Creator<Bird>() {
@Override
public Bird createFromParcel(Parcel source) {
//Log.i(ReferenceGuide.LOGTAG, "createFromParcel");
return new Bird(source);
}
@Override
public Bird[] newArray(int size) {
//Log.i(ReferenceGuide.LOGTAG, "newArray");
return new Bird[size];
}
};
编辑:这是logcat输出:
02-28 15:43:38.048: W/dalvikvm(16350): threadid=1: thread exiting with uncaught exception (group=0x41cfe468)
02-28 15:43:38.058: E/AndroidRuntime(16350): FATAL EXCEPTION: main
02-28 15:43:38.058: E/AndroidRuntime(16350): java.lang.NullPointerException
02-28 15:43:38.058: E/AndroidRuntime(16350): at com.example.birdnote.ReferenceGuide.onListItemClick(ReferenceGuide.java:89)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.widget.AbsListView.performItemClick(AbsListView.java:1150)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.widget.ListView.performItemClick(ListView.java:4397)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2985)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.widget.AbsListView$1.run(AbsListView.java:3671)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.os.Handler.handleCallback(Handler.java:615)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.os.Handler.dispatchMessage(Handler.java:92)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.os.Looper.loop(Looper.java:155)
02-28 15:43:38.058: E/AndroidRuntime(16350): at android.app.ActivityThread.main(ActivityThread.java:5454)
02-28 15:43:38.058: E/AndroidRuntime(16350): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 15:43:38.058: E/AndroidRuntime(16350): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 15:43:38.058: E/AndroidRuntime(16350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
02-28 15:43:38.058: E/AndroidRuntime(16350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
02-28 15:43:38.058: E/AndroidRuntime(16350): at dalvik.system.NativeStart.main(Native Method)
编辑2:
我设置的代码Birds
:
private List<Bird> birds;
// create reference to database
BirdsDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reference_guide);
// open connection to db
datasource = new BirdsDataSource(this);
datasource.open();
// get list of birds from db
List<Bird> birds = datasource.findAll();
if(birds.size() == 0) {
createData();
birds = datasource.findAll();
}
// set view adapter
ArrayAdapter<Bird> adapter = new CustomBaseAdapter(this, birds);
setListAdapter(adapter);
}
列表视图使用Birds
答案 0 :(得分:0)
替换此行
Bird bird = birds.get(position);
通过
Bird bird = (Bird)l.getAdapter().getItemAtPosition(position);