{"id":2251,"date":"2022-08-30T15:23:18","date_gmt":"2022-08-30T15:23:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/app-crashing-when-using-fragments-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:18","modified_gmt":"2022-08-30T15:23:18","slug":"app-crashing-when-using-fragments-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/app-crashing-when-using-fragments-collection-of-common-programming-errors\/","title":{"rendered":"App crashing when using fragments-Collection of common programming errors"},"content":{"rendered":"<p>Im working with fragments and..<\/p>\n<p>Ive got a problem in my code that I just cant find.<\/p>\n<p>The logcat points at this piece of code, in one of my fragments:<\/p>\n<pre><code>    @Override\npublic View onCreateView(LayoutInflater inflater, ViewGroup container,\n        Bundle savedInstanceState) {\n    return inflater.inflate(R.layout.activity_main, container, false);\n}\n<\/code><\/pre>\n<p>My main class (the FragmentActivity):<\/p>\n<pre><code>import android.app.Activity;\nimport android.os.Bundle;\nimport android.support.v4.app.Fragment;\nimport android.support.v4.app.FragmentActivity;\nimport android.support.v4.app.FragmentTransaction;\nimport android.view.View;\n\npublic class Fragments extends FragmentActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_fragments);\n\n        MainActivity fragment = new MainActivity();\n        FragmentTransaction transaction = getSupportFragmentManager()\n                .beginTransaction();\n        transaction.add(R.id.fragment_place, fragment);\n        transaction.commit();\n    }\n\n    public void onSelectFragment(View view) {\n\n        Fragment newFragment;\n\n        if (view == findViewById(R.id.add)) {\n            newFragment = new Add();\n        } else if (view == findViewById(R.id.map)) {\n            newFragment = new MainActivity();\n        } else {\n            newFragment = new MainActivity();\n        }\n\n        FragmentTransaction transaction = getSupportFragmentManager()\n                .beginTransaction();\n        transaction.replace(R.id.fragment_place, newFragment);\n        transaction.addToBackStack(null);\n        transaction.commit();\n    }\n\n}\n<\/code><\/pre>\n<p>and the logcat:<\/p>\n<pre><code>09-30 00:02:52.363: E\/AndroidRuntime(32284): FATAL EXCEPTION: main\n09-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\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.app.ActivityThread.access$600(ActivityThread.java:142)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.os.Handler.dispatchMessage(Handler.java:99)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.os.Looper.loop(Looper.java:137)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.app.ActivityThread.main(ActivityThread.java:4931)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at java.lang.reflect.Method.invokeNative(Native Method)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at java.lang.reflect.Method.invoke(Method.java:511)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at dalvik.system.NativeStart.main(Native Method)\n09-30 00:02:52.363: E\/AndroidRuntime(32284): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at com.example.free.MainActivity.onCreateView(MainActivity.java:124)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.app.Activity.performStart(Activity.java:5018)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    ... 11 more\n09-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\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.Fragment.instantiate(Fragment.java:402)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.Fragment.instantiate(Fragment.java:377)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)\n09-30 00:02:52.363: E\/AndroidRuntime(32284):    ... 24 more\n<\/code><\/pre>\n<p>The class &#8220;MainActivity&#8221; is very long so I pasted it using pasebin:<\/p>\n<p>http:\/\/pastebin.com\/Lt3wbNzD<\/p>\n<p>Thanks for your assistance!<\/p>\n<p><strong>Edit:<\/strong><\/p>\n<pre><code>private GoogleMap map;\n\nFragmentTransaction transaction = getChildFragmentManager().beginTransaction();\n        transaction.add(R.id.map, map).commit();\n<\/code><\/pre>\n<p>it shows an error:<\/p>\n<blockquote>\n<p>The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, GoogleMap)<\/p>\n<\/blockquote>\n<ol>\n<li>\n<pre><code>Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment\n<\/code><\/pre>\n<p>You can&#8217;t create a fragment inside a fragment using xml. You haven&#8217;t posted the xml for the fragment, but I&#8217;m guessing you have a map fragment in there. You can still add it, but you need to add it dynamically using <code>getChildFragmentManager()<\/code><\/p>\n<\/li>\n<li>\n<p>With support library you should use FrameLayout in xml as fragment holder instead of fragment like this:<\/p>\n<pre><code>\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You have to use That<\/p>\n<p><code>return inflater.inflate(R.layout.activity_main,null);<\/code><\/p>\n<p>you don`t have parent As it Fragment Transaction ..<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-05 09:32:41. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2251","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2251"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2251\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}