Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to provide support for Fragments and ActionBar for App which supports Android 2.2+ devices?
    text
    copied!<p>I have visited so many similar links like this i.e <a href="https://stackoverflow.com/questions/6528691/fragments-in-android-2-2-1-2-3-2-0-is-this-possible">1</a>, <a href="https://stackoverflow.com/questions/7176841/android-fragment-api-for-api-level-11">2</a> followed various examples / links i.e. <a href="http://mobile.tutsplus.com/tutorials/android/android-sdk_fragments/" rel="nofollow noreferrer">this one</a> and <a href="http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/" rel="nofollow noreferrer">this one</a> mainly I have followed this post from <a href="http://www.vogella.com/articles/Android/article.html#fragments_tutorial" rel="nofollow noreferrer">Vogella</a> that too multiple times, Every time I get same exception.... What could be the reason ?</p> <pre><code>07-27 12:00:33.431: E/AndroidRuntime(663): FATAL EXCEPTION: main 07-27 12:00:33.431: E/AndroidRuntime(663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myexample.fragmentdemoexample/com.myexample.fragmentsupportexample.MainFragmentActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.access$600(ActivityThread.java:123) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.os.Handler.dispatchMessage(Handler.java:99) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.os.Looper.loop(Looper.java:137) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.main(ActivityThread.java:4424) 07-27 12:00:33.431: E/AndroidRuntime(663): at java.lang.reflect.Method.invokeNative(Native Method) 07-27 12:00:33.431: E/AndroidRuntime(663): at java.lang.reflect.Method.invoke(Method.java:511) 07-27 12:00:33.431: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 07-27 12:00:33.431: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 07-27 12:00:33.431: E/AndroidRuntime(663): at dalvik.system.NativeStart.main(Native Method) 07-27 12:00:33.431: E/AndroidRuntime(663): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 07-27 12:00:33.431: E/AndroidRuntime(663): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.Activity.setContentView(Activity.java:1835) 07-27 12:00:33.431: E/AndroidRuntime(663): at com.myexample.fragmentsupportexample.MainFragmentActivity.onCreate(MainFragmentActivity.java:14) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.Activity.performCreate(Activity.java:4465) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 07-27 12:00:33.431: E/AndroidRuntime(663): ... 11 more 07-27 12:00:33.431: E/AndroidRuntime(663): Caused by: java.lang.ClassCastException: com.myexample.fragmentdemoexample.MyListFragment cannot be cast to android.support.v4.app.Fragment 07-27 12:00:33.431: E/AndroidRuntime(663): at android.support.v4.app.Fragment.instantiate(Fragment.java:388) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.support.v4.app.Fragment.instantiate(Fragment.java:363) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264) 07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669) 07-27 12:00:33.431: E/AndroidRuntime(663): ... 21 more </code></pre> <p><strong>EDIT</strong> Here is my full code //MainActivity</p> <pre><code>public class MainFragmentActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_fragment); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main_fragment, menu); return true; } } </code></pre> <p>//DetailActivity</p> <pre><code>public class DetailActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Need to check if Activity has been switched to landscape mode // If yes, finished and go back to the s tart Activity if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); return; } setContentView(R.layout.details_activity_layout); Bundle extras = getIntent().getExtras(); if (extras != null) { String s = extras.getString("value"); TextView view = (TextView) findViewById(R.id.detailsText); view.setText(s); } } } </code></pre> <p>//MyListFragment</p> <pre><code>public class MyListFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" }; ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } @Override public void onListItemClick(ListView l, View v, int position, long id) { String item = (String) getListAdapter().getItem(position); DetailFragment fragment = (DetailFragment) (getActivity().getSupportFragmentManager()).findFragmentById(R.id.detailFragment); if (fragment != null &amp;&amp; fragment.isInLayout()) { fragment.setText(item); } else { Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class); intent.putExtra("value", item); startActivity(intent); } } } </code></pre> <p>//DetailsFragment</p> <pre><code>public class DetailFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("Test", "hello"); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.details, container, false); return view; } public void setText(String item) { TextView view = (TextView) getView().findViewById(R.id.detailsText); view.setText(item); } } </code></pre> <p>//Layouts are //activity_main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;fragment android:id="@+id/listFragment" android:layout_width="150dip" android:layout_height="match_parent" android:layout_marginTop="?android:attr/actionBarSize" class="com.myexample.fragmentdemoexample.MyListFragment" &gt; &lt;/fragment&gt; &lt;fragment android:id="@+id/detailFragment" android:layout_width="match_parent" android:layout_height="match_parent" class="com.myexample.fragmentdemoexample.DetailFragment" &gt; &lt;!-- Preview: layout=@layout/details --&gt; &lt;/fragment&gt; &lt;/LinearLayout&gt; </code></pre> <p>//details.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/detailsText" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_horizontal|center_vertical" android:layout_marginTop="20dip" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="30dip" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>and code for layout-port is similar to as mentioned on Vogella's post with appropriate changes</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload