android.widget.LinearLayout can not be cast to android.widget.ListView

1

While running my application, this error appears:

  

05-28 05: 33: 33.773: E / AndroidRuntime (28824): java.lang.RuntimeException: Unable to start activity ComponentInfo {br.exampleactionbar / br.exampleactionbar.MainActivity}: java.lang.ClassCastException: android. widget.LinearLayout can not be cast to android.widget.ListView.

My Adapter class :

public class NoticeAdapter extends BaseAdapter {

    private List<Notice> itemList;
    private Context context;
    private LayoutInflater inflater;

    public NoticeAdapter(List<Notice> itemList, Context ctx) {
        this.itemList = itemList;
        this.context = ctx;       
    }

    public int getCount() {
        if (itemList != null)
            return itemList.size();
        return 0;
    }

    public Notice getItem(int position) {
        if (itemList != null)
            return itemList.get(position);
        return null;
    }

    public long getItemId(int position) {
        if (itemList != null)
            return itemList.get(position).hashCode();
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;

        if(view == null) {           
            view = inflater.inflate(R.layout.list_notice, null);
            holder = new ViewHolder();
            view.setTag(holder);

            holder.tvId = (TextView) view.findViewById(R.id.title);
            holder.tvDesc = (TextView) view.findViewById(R.id.description);
            holder.tvPtime = (TextView) view.findViewById(R.id.publication_time);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.tvId.setText(itemList.get(position).getTitle());
        holder.tvDesc.setText(itemList.get(position).getDescription());
        holder.tvPtime.setText(itemList.get(position).getPublicationTime());

       return view;
    }

    private static class ViewHolder {
        TextView tvId;
        TextView tvDesc;
        TextView tvPtime;
    }

    public List<Notice> getItemList() {
        return itemList;
    }

    public void setItemList(List<Notice> itemList) {
        this.itemList = itemList;
    }

}

My Fragment with ListView :

public class Fragment1 extends Fragment {
private List<Notice> result = new ArrayList<Notice>();
private NoticeAdapter adpt;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    ListView lView = (ListView) inflater.inflate(R.layout.list_notice, container, false);        

    JsonArrayRequest jReq = new JsonArrayRequest("http://192.168.1.101:3000/notices",
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            Notice notice = new Notice();
                            notice.setId(response.getJSONObject(i).getInt("id"));
                            notice.setPicture(response.getJSONObject(i).getString("picture"));
                            notice.setPublicationTime(response.getJSONObject(i).getString("publication_time"));
                            notice.setReducedDescription(response.getJSONObject(i).getString("reduced_description"));
                            notice.setReference(response.getJSONObject(i).getString("reference"));
                            notice.setTitle(response.getJSONObject(i).getString("title"));
                            result.add(notice);
                        } catch (JSONException e) {
                            Toast.makeText(getActivity(), "Falha com a conexão de internet", Toast.LENGTH_LONG).show();
                        }
                    }
                    adpt.setItemList(result);
                    adpt.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    Volley.newRequestQueue(getActivity().getApplicationContext()).add(jReq);
    adpt = new NoticeAdapter(result, this.getActivity());
    lView.setAdapter(adpt); 

    return lView;
}
}

My xml list:

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

    <ListView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@android:id/list" />

</LinearLayout>

Item xml:

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

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/publication_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

I'm starting in the Android development, so I do not have much notion of what may be wrong.

    
asked by anonymous 28.05.2015 / 11:03

1 answer

1

The error is due to the fact that you are trying to "transform" a LinearLayout into a ListView .

The onCreateView() method is to inform the Fragment class what the layout should use, which in this case is R.layout.list_notice . On the other hand you need to have a reference to the ListView that is within Layout .

What are you doing on this line:

ListView lView = (ListView)inflater.inflate(R.layout.list_notice, container, false);

It has to be done in two:

//Primeiro construir o Layout
View view = inflater.inflate(R.layout.list_notice, container, false);
// depois obter a referência da ListView
ListView lView = (ListView)view.findViewById(android.R.id.list);

The method should return the Layout and not the ListView :
change

return lView;

for

return view;
    
28.05.2015 / 12:01