美好的一天!
我很难解决这个问题。我尝试实现接口,parcelable对象和bundle只是为了与片段进行通信。但是在我运行我的代码之后,每次在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();
}
}
My MainActivity Class部分我在接口方法中实现了Bundles(_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);
}
}
每次运行程序时,它总是将错误指向Fragment clas中的onCreate。任何人都可以向我解释这个错误的根本原因并建议我防止这种情况发生的想法吗?
提前致谢!
答案 0 :(得分:0)
您的错误在这里
lv=(ListView)getActivity().findViewById(R.id.listview_genesis);
其中,findViewById
返回null,因为我假设Activity XML布局不包含ListView,而是Fragment XML布局。
您可以在onCreateView
,
@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);
lv = v.findViewById(R.id.listview_genesis);
if (listeners != null) {
Bundle bundle=listeners.displayData();
if (bundle.isEmpty()) {
Log.e("BUNDLE","EMPTY");
} else {
verseList= bundle.getParcelableArrayList("verseValues");
verseAdapter= new VerseAdapter(getActivity(), verseList);
lv.setAdapter(verseAdapter);
}
}
return v;
}
实际上,displayData()
对于该方法来说都是一个糟糕的名称,而且大多数都是错误的方法来做你想要的,因为你创建了一个new GenesisFragment
,设置了参数,然后对它做了什么。
它应该返回Fragment
,而不是Bundle
,并且它不会显示数据,它只是返回一个值。
看起来你试图实现instantiating a Fragment之类的东西,其中newInstance
方法不在Activity类中,而是Fragment类,然后从Activity中将该实例加载到FragmentManager中。