android.widget.Linearlayout cannot be cast to android.widget.Listview

Asked

Viewed 279 times

1

When running my application, this error appears:

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

My class Adapter:

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;
    }

}

Man Fragment with the 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>

Xml item:

<?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 Android development, so I don’t have much sense of what might be wrong.

1 answer

1


The mistake is due to the fact that you are trying to "transform" a Linearlayout in a Listview.

The method onCreateView() serves to inform the class Fragment which the layout it must use which, in this case, is R.layout.list_notice.
On the other hand you need to have a reference to Listview that’s inside the 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 must return the Layout and not the Listview:
alter

return lView;

for

return view;
  • made the changes, but the error still persists.

  • What’s the name XML that has the Listview

  • By mistake I put View view = (ListView) inflater.inflate(... but it has to be View view = inflater.inflate(...

  • worked, the app ran. But it didn’t render listview with JSON. There’s something wrong with Fragment?

  • I am running the app by my mobile phone, and using wifi, to request JSON, which is on my notebook. Is the error there?

  • As this is already another problem I suggest you create another question. Before doing so check if something is being returned from the service.

  • The creation of listview, Adapter and request with Volley are right, right?

  • I’m sorry I can’t help you because I’ve never used *Volley".

  • will I need to implement some other Fragment method, for the list, for it to be displayed? Like the onclick method something and etc?

  • Check whether the method onResponse() is being called and if the variable response has something

  • can I test with Toast? or System.out?

  • Can with Toast.

  • I tested with Toast, nothing came up. I don’t think he’s being able to create Listview, not because of the request. Is there any method missing to be implemented?

  • It seems that the problem is like the Volley is being used. As I have never used it, I cannot help.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.