美好的一天!不能將Arraylist <Object>的值傳遞給片段
我很難得到這個問題。我嘗試實現接口,parcelable對象和bundles,以便與片段進行通信。但是在我運行我的代碼之後,它每次都會在Fragment類中發生NullPointerException。這裏的錯誤:
07-31 18:09:42.052 4776-4776/bwrt.monsters.android.com.blesswhoreadthis E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{bwrt.monsters.android.com.blesswhoreadthis/bwrt.monsters.android.com.blesswhoreadthis.Activity.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at wrt.monsters.android.com.blesswhoreadthis.Fragment.GenesisFragment.onCreate(GenesisFragment.java:58)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:796)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
at android.app.Activity.performStart(Activity.java:5017)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
這裏是我的片段類:
public class GenesisFragment extends Fragment {
Context mContext;
ListView lv;
ArrayList<Bible> verseList=null;
VerseAdapter verseAdapter;
displayDataInterface listeners;
public interface displayDataInterface {
Bundle displayData();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof displayDataInterface) {
this.listeners= (displayDataInterface) context;
}else {
throw new ClassCastException(context.toString()+" must implement the NEEDED INTERFACE!");
}
}
/** @Override */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle=listeners.displayData();
if (bundle.isEmpty()) {
Log.e("BUNDLE","EMPTY");
}else {
verseList= bundle.getParcelableArrayList("verseValues");
}
verseAdapter= new VerseAdapter(mContext,verseList);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_genesis, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
lv=(ListView)getActivity().findViewById(R.id.listview_genesis);
lv.setAdapter(verseAdapter);
verseAdapter.notifyDataSetChanged();
}
}
我實現捆綁接口的方法內(_bibleValues-一個ArrayList)我的MainActivity類別部分:
/** Interface Generated Methods*/
@Override
public Bundle displayData() {
GenesisFragment gFragment= new GenesisFragment();
Bundle args= new Bundle();
args.putParcelableArrayList("verseValues",_bibleValues); //Make the Object class Parcelable 1st
gFragment.setArguments(args);
return args;
}
我的物品等級:
public class Bible implements Parcelable{
private int id;
private int book;
private int chapter;
private int verseCount;
private String verseContent;
/** Constructors */
public Bible(int _id,int _book, int _chapter,int _verseCount, String _verseContent) {
this.id=_id;
this.book=_book;
this.chapter=_chapter;
this.verseCount=_verseCount;
this.verseContent=_verseContent;
}
protected Bible(Parcel in) {
id = in.readInt();
book = in.readInt();
chapter = in.readInt();
verseCount = in.readInt();
verseContent = in.readString();
}
public static final Creator<Bible> CREATOR = new Creator<Bible>() {
@Override
public Bible createFromParcel(Parcel in) {
return new Bible(in);
}
@Override
public Bible[] newArray(int size) {
return new Bible[size];
}
};
/** Getters and Setters */
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getBook() {
return book;
}
public void setBook(int book) {
this.book = book;
}
public int getChapter() {
return chapter;
}
public void setChapter(int chapter) {
this.chapter = chapter;
}
public int getVerseCount() {
return verseCount;
}
public void setVerseCount(int verseCount) {
this.verseCount = verseCount;
}
public String getVerseContent() {
return verseContent;
}
public void setVerseContent(String verseContent) {
this.verseContent = verseContent;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(book);
dest.writeInt(chapter);
dest.writeInt(verseCount);
dest.writeString(verseContent);
}
}
每次我運行它始終指向的onCreate錯誤的片段CLAS程序。任何人都可以向我解釋這個錯誤的根源,並建議我的想法,以防止這種情況發生?
在此先感謝!
'_bibleValues'的類型是什麼? – skywall
@ skywall-一個對象先生。因爲我有一個可以分類的對象,只是爲了實現Bundle方法。 – PhilipJ
我的對象由4個整數和1個字符串組成。 – PhilipJ