Im working with fragments and..
Ive got a problem in my code that I just cant find.
The logcat points at this piece of code, in one of my fragments:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main, container, false);
}
My main class (the FragmentActivity):
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
public class Fragments extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
MainActivity fragment = new MainActivity();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_place, fragment);
transaction.commit();
}
public void onSelectFragment(View view) {
Fragment newFragment;
if (view == findViewById(R.id.add)) {
newFragment = new Add();
} else if (view == findViewById(R.id.map)) {
newFragment = new MainActivity();
} else {
newFragment = new MainActivity();
}
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_place, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
and the logcat:
09-30 00:02:52.363: E/AndroidRuntime(32284): FATAL EXCEPTION: main
09-30 00:02:52.363: E/AndroidRuntime(32284): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.Fragments}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.os.Looper.loop(Looper.java:137)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.app.ActivityThread.main(ActivityThread.java:4931)
09-30 00:02:52.363: E/AndroidRuntime(32284): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 00:02:52.363: E/AndroidRuntime(32284): at java.lang.reflect.Method.invoke(Method.java:511)
09-30 00:02:52.363: E/AndroidRuntime(32284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-30 00:02:52.363: E/AndroidRuntime(32284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-30 00:02:52.363: E/AndroidRuntime(32284): at dalvik.system.NativeStart.main(Native Method)
09-30 00:02:52.363: E/AndroidRuntime(32284): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-30 00:02:52.363: E/AndroidRuntime(32284): at com.example.free.MainActivity.onCreateView(MainActivity.java:124)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.app.Activity.performStart(Activity.java:5018)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-30 00:02:52.363: E/AndroidRuntime(32284): ... 11 more
09-30 00:02:52.363: E/AndroidRuntime(32284): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
09-30 00:02:52.363: E/AndroidRuntime(32284): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
09-30 00:02:52.363: E/AndroidRuntime(32284): ... 24 more
The class "MainActivity" is very long so I pasted it using pasebin:
http://pastebin.com/Lt3wbNzD
Thanks for your assistance!
Edit:
private GoogleMap map;
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.map, map).commit();
it shows an error:
The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, GoogleMap)
Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
You can't create a fragment inside a fragment using xml. You haven't posted the xml for the fragment, but I'm guessing you have a map fragment in there. You can still add it, but you need to add it dynamically using getChildFragmentManager()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With