この問題が発生するのは苦労します。私は、フラグメントと通信するために、インタフェース、パーセルブルオブジェクト、バンドルを実装しようとしています。しかし、コードを実行した後は、フラグメントクラスで毎回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();
}
}
マイMainActivityクラスの部分(_bibleValues-のArrayList):
ここでエラーがあります/** 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);
}
}
毎回私はそれは常にフラグメントCLASでのonCreateでエラーを指してプログラムを実行します。誰もが私にこのエラーの根本的な原因を説明し、これを防ぐためのアイデアをアドバイスできますか?
ありがとうございました!
'_bibleValues'の型は何ですか? – skywall
@ skywall- an Object sir。 Bundleメソッドを実装するだけで、私はオブジェクトをパーセル化することができます。 – PhilipJ
myオブジェクトは4つの整数と1つの文字列で構成されます。 – PhilipJ