Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Bar Sherlock + ListFragments + Fragments

I have a problem with the compatibility of the fragments in version 2.2. I use "Sherlock Action Bar" to create the actionbar, but I can not 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 this:

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 this:

<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" conteins this:

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 put much code, but not because it is wrong.

Thank you very much for your help.

like image 333
Dracknes Avatar asked Dec 27 '22 11:12

Dracknes


2 Answers

My main_activity contains this:

Please inherit from a Sherlock class, such as SherlockActivity, if you intend to use ActionBarSherlock. If you also intend to use fragments, please inherit from SherlockFragmentActivity.

To quote the ActionBarSherlock documentation:

When creating an activity to use the action bar on all versions of Android, you must declare your activity to extend any of the activity classes that start with 'Sherlock' (e.g., SherlockActivity, SherlockFragmentActivity).

like image 129
CommonsWare Avatar answered Jan 12 '23 07:01

CommonsWare


In order to work with compability fragments and ABS you must extend SherlockFragmentActivity

public class MainActivity extends SherlockFragmentActivity {
like image 41
Yaroslav Mytkalyk Avatar answered Jan 12 '23 08:01

Yaroslav Mytkalyk