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