-1

Good Day!

Im having a hard time on getting this issue. I try to implement interface,parcelable objects and bundles just to communicate with the fragments. But after i run my code, it goes NullPointerException everytime in the Fragment Class. Here's the Error:

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) 

Here's my Fragment Class:

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 part which i implemented Bundles inside the interface methods (_bibleValues- an 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;
}

My Object Class:

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);
}

}

Everytime i run the program it always pointing the error in onCreate in the Fragment clas. Can anyone explain to me the root cause of this error and advice me ideas to prevent this from occuring?

Thanks in Advance!

1 Answers1

0

Your error is here

lv=(ListView)getActivity().findViewById(R.id.listview_genesis);

Where, findViewById has returned you null because I assume the Activity XML layout doesn't contain that ListView, but instead the Fragment XML layout does.


You can do most of all these operations inside 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;

}

Realistically, displayData() is both a poor name for that method, and mostly the wrong way to do what you want because you make a new GenesisFragment, set the arguments, then do nothing with it.

It should return the Fragment, not the Bundle, and it isn't displaying data, it's just returning a value.

Looks like your trying to achieve something like instantiating a Fragment, where the newInstance method is not in the Activity class, but the Fragment class, and you load that instance into the FragmentManager from the Activity.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245