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.
made the changes, but the error still persists.
– Vinícius Venancio dos Santos
What’s the name XML that has the Listview
– ramaral
By mistake I put
View view = (ListView) inflater.inflate(...but it has to beView view = inflater.inflate(...– ramaral
worked, the app ran. But it didn’t render listview with JSON. There’s something wrong with Fragment?
– Vinícius Venancio dos Santos
I am running the app by my mobile phone, and using wifi, to request JSON, which is on my notebook. Is the error there?
– Vinícius Venancio dos Santos
As this is already another problem I suggest you create another question. Before doing so check if something is being returned from the service.
– ramaral
The creation of listview, Adapter and request with Volley are right, right?
– Vinícius Venancio dos Santos
I’m sorry I can’t help you because I’ve never used *Volley".
– ramaral
will I need to implement some other Fragment method, for the list, for it to be displayed? Like the onclick method something and etc?
– Vinícius Venancio dos Santos
Check whether the method
onResponse()is being called and if the variableresponsehas something– ramaral
can I test with Toast? or System.out?
– Vinícius Venancio dos Santos
Can with Toast.
– ramaral
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?
– Vinícius Venancio dos Santos
It seems that the problem is like the Volley is being used. As I have never used it, I cannot help.
– ramaral