Application crashes when using fragments

Im working with fragments and ...

I have a problem with code that I just can't find.

The log code points to this piece of code in one of my snippets:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.activity_main, container, false); } 

My main class (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 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 "MainActivity" class is very long, so I inserted it with pasebin:

http://pastebin.com/Lt3wbNzD

Thank you for your help!

Edit:

 private GoogleMap map; FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.map, map).commit(); 

error displayed:

The add (int, Fragment) method in the FragmentTransaction type is not applicable for arguments (int, GoogleMap)

+6
source share
3 answers
 Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment 

You cannot create a fragment inside a fragment using xml. You have not placed the xml for the fragment, but I assume that you have a map fragment. You can add it, but you need to add it dynamically using getChildFragmentManager()

+2
source

In the support library, you should use FrameLayout in xml as the owner of the fragment instead of the fragment, for example:

 <FrameLayout android:id="@+id/fragment_place" ... /> 
+1
source

You must use that

return inflater.inflate(R.layout.activity_main,null);

you do not have a parent. How this happens with fragmentation.

0
source

Source: https://habr.com/ru/post/954902/


All Articles