Sherlock Action Pane + List Fragments + Fragments

I have a fragment compatibility issue in version 2.2. I use the Sherlock Action Bar to create an action bar, but I cannot create two fragments. I get this error all the time:

01-07 12:51:56.124: E/AndroidRuntime(475): FATAL EXCEPTION: main 01-07 12:51:56.124: E/AndroidRuntime(475): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pruebasherlock_v4/com.example.pruebasherlock_v4.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 01-07 12:51:56.124: E/AndroidRuntime(475): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.os.Handler.dispatchMessage(Handler.java:99) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.os.Looper.loop(Looper.java:123) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-07 12:51:56.124: E/AndroidRuntime(475): at java.lang.reflect.Method.invokeNative(Native Method) 01-07 12:51:56.124: E/AndroidRuntime(475): at java.lang.reflect.Method.invoke(Method.java:521) 01-07 12:51:56.124: E/AndroidRuntime(475): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-07 12:51:56.124: E/AndroidRuntime(475): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-07 12:51:56.124: E/AndroidRuntime(475): at dalvik.system.NativeStart.main(Native Method) 01-07 12:51:56.124: E/AndroidRuntime(475): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 01-07 12:51:56.124: E/AndroidRuntime(475): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 01-07 12:51:56.124: E/AndroidRuntime(475): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.app.Activity.setContentView(Activity.java:1647) 01-07 12:51:56.124: E/AndroidRuntime(475): at com.example.pruebasherlock_v4.MainActivity.onCreate(MainActivity.java:12) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 01-07 12:51:56.124: E/AndroidRuntime(475): ... 11 more 01-07 12:51:56.124: E/AndroidRuntime(475): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.pruebasherlock_v4-1.apk] 01-07 12:51:56.124: E/AndroidRuntime(475): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) 01-07 12:51:56.124: E/AndroidRuntime(475): at java.lang.ClassLoader.loadClass(ClassLoader.java:573) 01-07 12:51:56.124: E/AndroidRuntime(475): at java.lang.ClassLoader.loadClass(ClassLoader.java:532) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.view.LayoutInflater.createView(LayoutInflater.java:466) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544) 01-07 12:51:56.124: E/AndroidRuntime(475): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 01-07 12:51:56.124: E/AndroidRuntime(475): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 01-07 12:51:56.124: E/AndroidRuntime(475): ... 20 more 

My main_activity contains the following:

 package com.example.pruebasherlock_v4; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

My main.xml contains the following:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <fragment android:id="@+id/listaIzq" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" class="com.example.pruebasherlock_v4.Lista"/> <fragment android:id="@+id/detallesDer" android:layout_weight="3" android:layout_width="0dp" android:layout_height="fill_parent" class="com.example.pruebasherlock_v4.Detalle" /> </LinearLayout> 

And the class "lista" and "Detalle" contains the following:

Lista:

 package com.example.pruebasherlock_v4; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import com.actionbarsherlock.app.SherlockListFragment; public class Lista extends SherlockListFragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override //se sobreescribe este método como el que siempre "carga" la información a mostrar en el fragment 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<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } @Override //al hacer tap en algun elemento de la vista, setea el texto del elemento en el nuevo fragment public void onListItemClick(ListView l, View v, int position, long id) { String item = (String) getListAdapter().getItem(position); Detalle fragment = (Detalle) getFragmentManager().findFragmentById(R.id.detallesDer); if (fragment != null && fragment.isInLayout()) { fragment.setText(item); } else { /*Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class); intent.putExtra("value", item); startActivity(intent);*/ } } } 

Detalle:

 package com.example.pruebasherlock_v4; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.actionbarsherlock.app.SherlockFragment; public class Detalle extends SherlockFragment { @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.detalle, container, false); return view; } public void setText(String item) { TextView view = (TextView) getView().findViewById(R.id.textoDetalle); view.setText(item); } } 

I feel a lot of code, but not because it is wrong.

Many thanks for your help.

+4
source share
2 answers

My main_activity contains the following:

Inherit the Sherlock class, such as SherlockActivity , if you intend to use an ActionBarSherlock. If you are also going to use fragments, inherit them from SherlockFragmentActivity .

To quote ActionBarSherlock documentation :

When creating an action to use the action bar in all versions of Android, you must declare your activity to extend any of the activity classes starting with Sherlock (for example, SherlockActivity, SherlockFragmentActivity).

+6
source

To work with compilation fragments and ABS, you must extend the SherlockFragmentActivity function

 public class MainActivity extends SherlockFragmentActivity { 
+4
source

All Articles