Reputation: 497
I've done a few simple apps, but still new to Android development. I've been trying to make fragments work but my app just seems to crash every time I try to load a fragment. I'm trying to make a game. The way I had structured it was that every menu screen would be a fragment(main menu, options, ingame). I went on and made my research about how to program a fragment. Found a few good articles and videos. Pretty much they all do kinda the same thing, but for them it seems to work. It really didn't work for me. They way I wanted this to work was, load one activity and on that activity change or replace fragments according to the menu.
On crash, logcat, sometimes displays the error and sometimes it doesn't.
Let me know if you need the XML file(highly doubt it. Not adding fragment with fragment tag).
This is what I've tried so far:
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment blankFragment= new BlankFragment();
fragmentTransaction.add(android.R.id.content,blankFragment);
fragmentTransaction.commit();
}
}
BlankFragment:
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static Button startBtn,
optionsBtn;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_blank, container, false);
// Inflate the layout for this fragment
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(Uri uri);
}
LogCat:
07-07 23:03:20.120 8978-8978/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.marybel.myapplication, PID: 8978
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.marybel.myapplication/com.example.marybel.myapplication.MainActivity}: java.lang.ClassCastException: com.example.marybel.myapplication.MainActivity@3275aea8 must implement OnFragmentInteractionListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.example.marybel.myapplication.MainActivity@3275aea8 must implement OnFragmentInteractionListener
at com.example.marybel.myapplication.BlankFragment.onAttach(BlankFragment.java:83)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Any explanation and suggestions would be greatly appreciated, thanks.
Upvotes: 0
Views: 91
Reputation: 497
Managed to get it working, they way I added the fragment was by right clicking on Layout>New>Fragment. Seem like one of the auto generated methods or overrides made it crash. Deleted everything except the OnCreateView
and worked just fine. Thanks
Upvotes: 0
Reputation: 2066
On a first glimpse I see in Activity > onCreate() your setContentView()
is commented.
For adding any fragment into your activity, first thing beforehand is setting the activity view by calling setContentView()
.
Please move setContentView()
after super.onCreate(savedInstanceState)
and let know how is then, e.g.
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.YOUR_LAYOUT_FILE_NAME);
...
}
Upvotes: 1