0

I follow this ( http://www.learn2crack.com/2014/05/android-working-with-fragments.html ) tutorial about Fragment . When i run the project, i got an error like the title of this question Content has view with id attribute 'android.R.id.list' that is not a ListView class

this is list_fragment.xml

<?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="wrap_content"
        android:id="@+id/android:list"></ListView>
</LinearLayout>

text_fragment.xml

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40px"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:id="@+id/AndroidOS"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#ffffff"
        android:textSize="30px"
        android:id="@+id/Version"/>

</LinearLayout>

Text_fragment.java

public class Text_Fragment extends Fragment{


    TextView text, vers;

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){

       View view = inflater.inflate(R.layout.text_fragment, container, false);

       text = (TextView) view.findViewById(R.id.AndroidOS); 
       vers = (TextView) view.findViewById(R.id.Version);

       return view; 


   }

    public void change(String txt, String txt1){
        text.setText(txt);
        vers.setText(txt1);
    }
}

Menu_Fragment.java

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MenuFragment extends ListFragment {
    String[] AndroidOS = new String[] { "Cupcake","Donut","Eclair","Froyo","Gingerbread","Honeycomb","Ice Cream SandWich","Jelly Bean","KitKat" };
    String[] Version = new String[]{"1.5","1.6","2.0-2.1","2.2","2.3","3.0-3.2","4.0","4.1-4.3","4.4"};
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.list_fragment, container, false);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, AndroidOS);
        setListAdapter(adapter);
        return view;
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        TextFragment txt = (TextFragment)getFragmentManager().findFragmentById(R.id.fragment2);
        txt.change(AndroidOS[position],"Version : "+Version[position]);
        getListView().setSelector(android.R.color.holo_blue_dark);
    }
}

the part of Main.java

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

Sorry i post the code too long. Please suggest me. Thanks

Sen
  • 154
  • 1
  • 11
  • 1
    change your listview id from xml file android:id="@android:id/list" – Pratik Tank Oct 02 '15 at 18:19
  • but now, i got an error `android.widget.LinearLayout cannot be cast to android.widget.TextView` . people that faced same problem said it caused by id of LinearLayout? – Sen Oct 03 '15 at 00:35
  • Refactor(Rename) your Text_Fragment to TextFragment and clean and rebuild your project and uninstall app from device then try.Text_Fragment is used in MenuFragment see that. – Pratik Tank Oct 03 '15 at 14:26
  • sometimes, some tutorials in internet get an error that they can't suggest the solutions. Thanks guys!! – Sen Oct 03 '15 at 17:03

0 Answers0