适配器中ListView的Android错误:java.lang.ClassCastException

时间:2013-04-30 14:56:03

标签: android listview android-arrayadapter

我在显示列表视图时遇到此错误。问题在于适配器,但我找不到解决方案。这在我的logcat中:

FATAL EXCEPTION: main

    java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
    at com.client.alm.WorkItemAdapter.getView(WorkItemAdapter.java:47)
    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
    at android.widget.ListView.onMeasure(ListView.java:1158)
    at android.view.View.measure(View.java:15513)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)

这是我的适配器:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.client.alm.model.WorkItem;
import com.client.alm.model.WorkItemContainer;

public class WorkItemAdapter extends ArrayAdapter<WorkItem> {

    private WorkItemContainer container;
    private Context context;
    int layoutResourceId;

    public WorkItemAdapter(Context context, int layoutResourceId,
            WorkItemContainer container) {
        super(context, layoutResourceId,container.getWorkitems());
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.container = container;
    }

    static class ViewHolder {
        ImageView imageType;
        TextView type;
        TextView priority;
        ImageView imagePriority;
        TextView key;
        ImageView imageStatus;
        TextView status;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder();
            holder.imageType = (ImageView) row
                    .findViewById(R.id.imageTypeWorkitem);
            holder.type = (TextView) row.findViewById(R.id.typeWorkitem);
            holder.imagePriority = (ImageView) row
                    .findViewById(R.id.imagePriority);
            holder.key = (TextView) row.findViewById(R.id.key);
            holder.imageStatus = (ImageView) row.findViewById(R.id.imageStatus);
            holder.status = (TextView) row.findViewById(R.id.status);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        WorkItem workitem = container.getWorkitems().get(position);
        holder.type.setText(workitem.getType());
        // holder.imageType.setImageResource(workitem.getIcon());
        holder.priority.setText(workitem.getPriority());
        holder.key.setText(workitem.getKey());
        holder.status.setText(workitem.getStatus());

        return row;
    }

}

这是我的活动:

import java.lang.reflect.Type;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.client.alm.model.WorkItem;
import com.client.alm.model.WorkItemContainer;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class WorkItemActivity extends Activity {
    /** Called when the activity is first created. */

    // Progress Dialog
    private ProgressDialog pDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wokitemlist);

        new LoadWorkitem().execute();
    }

    /**
     * Background Async Task to Load all Workitems by making HTTP Request
     * */
    class LoadWorkitem extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(WorkItemActivity.this);
            pDialog.setMessage("Loading Workitems ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting JSON
         * */
        @Override
        protected String doInBackground(String... params) {
            RestClient rc = new RestClient(
                    "http://10.0.2.2:8080/SpringRestWebServices/rest/workitem/getworkitem");

            try {
                rc.Execute(RequestMethod.GET);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return rc.getResponse();
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        @Override
        protected void onPostExecute(String resultat) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();

            Gson gson = new Gson();
            WorkItemContainer container = gson.fromJson(resultat,
                    WorkItemContainer.class);

            /**
             * Updating parsed JSON data into ListView
             * */
            WorkItemAdapter adaptateur = new WorkItemAdapter(
                    getApplicationContext(), R.layout.workitemitem, container);

            ListView listViewWorkitem = (ListView) findViewById(R.id.list);

            listViewWorkitem.setAdapter(adaptateur);
            super.onPostExecute(resultat);
        }
    }

}

非常感谢你的帮助。提前致谢。

3 个答案:

答案 0 :(得分:6)

在您的适配器更改

LayoutInflater inflater = ((Activity) context).getLayoutInflater();

LayoutInflater inflater = LayoutInflater.from(context);

我也会将这一行从getView移到类构造函数中,所以你只需要调用它一次。

但无论如何,正如@geeth指出的那样,使用Activity上下文而不是Application来实例化适配器是一个很好的做法,否则你可能会有一些奇怪的行为(例如,如果你活动是主题,使用应用程序上下文将错误的主题应用于您的ListView行)

答案 1 :(得分:2)

您可能会发送应用程序context而不是活动context。 请说明哪一行出错。

答案 2 :(得分:2)

替换以下代码

 /**
         * Updating parsed JSON data into ListView
         * */
        WorkItemAdapter adaptateur = new WorkItemAdapter(
                getApplicationContext(), R.layout.workitemitem, container);

 /**
         * Updating parsed JSON data into ListView
         * */
        WorkItemAdapter adaptateur = new WorkItemAdapter(
               WorkItemActivity.this, R.layout.workitemitem, container);

您将解决您的问题。