James andresakis
James andresakis

Reputation: 5415

Android - ListFragment fails on setContentView

Ive been trying to get a listview working within a fragment all day. I dont know why but for some reason the way Im trying to do it, it just never wants to inflate the view. Im using actionbarsherlock if that makes a difference but I dont think so. Heres the class where my layout fails:

 public class GraffSearch extends FragmentActivity implements ActionBar.TabListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.searchlayout);//<-------------fails here

    final ActionBar ab = getSupportActionBar();

    for (int i = 1; i < 4; i++) {
        ab.addTab(ab.newTab().setText("Tab " + i).setTabListener(this));
    }


    ab.show();
    if (ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_TABS) {
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }



}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    //Toast.makeText(this, "tabs work "+tab.getPosition()+" user id is "+avrid, Toast.LENGTH_LONG).show();
    SearchListFrag fragment = (SearchListFrag) getSupportFragmentManager().findFragmentById(R.id.searchfrag);
    //fragment.updateUrl(tab.getPosition(), avrid);

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

}

and heres the layout were I call my fragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
>


<ListFragment
    android:id="@+id/searchfrag"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:name="graffit.main.SearchListFrag"
    android:layout_weight="1"></ListFragment>



</LinearLayout>

EDIT: my searchlistfrag class

public class SearchListFrag extends Fragment{

ListView markList;

public SearchListFrag(){};

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    markList = (ListView) inflater
               .inflate(R.id.searchlist, container, false);//<----------Fails Here
     markList.setAdapter(new OrderAdapter(
             MixView.mixContext.getBaseContext(),
             R.layout.searchinnerlistlayout, 
             MixView.dataView.dataHandler.markerList ));


    // return inflater.inflate(R.layout.searchlistlayout, container, false);
    return markList;            

    }


    @Override
    public void onPause() {

       super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    public void updateUrl(int tab, String avrid) {

    }

    private class OrderAdapter extends ArrayAdapter<Marker> {

        private ArrayList<Marker> marks;

        public OrderAdapter(Context context, int textViewResourceId, ArrayList<Marker> items) {
                super(context, textViewResourceId, items);
                this.marks = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.searchinnerlistlayout, null);
                }
              //  Order o = items.get(position);
                if (marks.get(position) != null) {
                        TextView title = (TextView) v.findViewById(R.id.title);
                        //TextView description = (TextView) v.findViewById(R.id.description);
                        if (title != null) {
                              title.setText("Title: "+marks.get(position).getTitle());                            }
                        //if(description != null){
                        //  description.setText("Description: "+ +marks.get(position).get);
                       // }
                        ImageView icon = (ImageView) v.findViewById(R.id.markimage);
                        icon.setImageBitmap(marks.get(position).getBitmap());
                }

                return v;
        }
}   
}

EDIT: my searchlistlayout layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     >
 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:name="@+id/searchlist"></ListView> //<----this is the id i use that fails

 </LinearLayout>

Ive tried a few different setups going back and forth from using a regular fragment and a listfragment with the same result. Heres the logcat.

EDIT: New log cat

03-18 15:34:45.939: E/AndroidRuntime(20704): FATAL EXCEPTION: main
03-18 15:34:45.939: E/AndroidRuntime(20704): java.lang.RuntimeException: Unable to start activity ComponentInfo{graffit.main/graffit.main.GraffSearch}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.access$1500(ActivityThread.java:124)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.os.Looper.loop(Looper.java:130)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.main(ActivityThread.java:3806)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at java.lang.reflect.Method.invoke(Method.java:507)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at dalvik.system.NativeStart.main(Native Method)
03-18 15:34:45.939: E/AndroidRuntime(20704): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentActivity.setContentView(FragmentActivity.java:421)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at graffit.main.GraffSearch.onCreate(GraffSearch.java:16)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
03-18 15:34:45.939: E/AndroidRuntime(20704):    ... 11 more
03-18 15:34:45.939: E/AndroidRuntime(20704): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050068 type #0x12 is not valid
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:1912)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.content.res.Resources.getLayout(Resources.java:746)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at graffit.main.SearchListFrag.onCreateView(SearchListFrag.java:28)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:660)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
03-18 15:34:45.939: E/AndroidRuntime(20704):    ... 19 more

Upvotes: 1

Views: 7096

Answers (1)

thaussma
thaussma

Reputation: 9886

The fragment import in your layout is wrong. Fragments are added to a layout with the <fragment> tag. Other than with Views you don't name the tag after the fully-qualified class name. The error occurs because Android tries to inflate a View with the type 'ListFragment', that doesn't exist.

Try:

<fragment
    android:id="@+id/searchfrag"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:name="graffit.main.SearchListFrag"
    android:layout_weight="1"></fragment>

The <fragment> has to be in lower case, <Fragment> won't work too.

Your mixing some things up.

1) The onCreate method of the fragment has to inflate the whole layout of the fragment. After inflating the Fragment layout you can get the ListView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View contentView = inflater.inflate(R.id.searchlist, container, false);     
    markList = (ListView) contentView.findViewById(R.id.searchlist);
    markList.setAdapter(new OrderAdapter(MixView.mixContext.getBaseContext(), R.layout.searchinnerlistlayout, MixView.dataView.dataHandler.markerList));

    return contentView;
}

2) You did not define the id of the ListView, you defined its name. Change the layout:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     >
 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/searchlist"></ListView> 
 </LinearLayout>

Upvotes: 4

Related Questions