0

我正在 android 中开发一个应用程序,其中包含带有图像的列表视图、webviews、我们在日常应用程序中使用的许多常见视图。我的应用程序运行正常,但是当我运行它 3 或 4 次时,它突然从当前活动中崩溃并转到上一个活动。我不知道为什么。它没有给出任何异常或错误。我再次启动该活动 3 或 4 次,它正常工作并再次崩溃。谁能告诉我为什么会发生。可能是内存管理。

主要是它在列表视图活动中崩溃了。但是,如果它在 listview 上使用数据加载而崩溃,则必须给出异常。我还使用过线程、imagesloader、viewholder。但是找不到问题所在!

ListView 的适配器是:

public class SavedAdapter extends BaseAdapter {

Context context;
JSONArray jsonArraySaved;
JSONObject jsonObjectSaved;
String user_id = "", distance = "";

public SavedAdapter(Context context, JSONArray jsonArraySaved) {
    this.context = context;
    this.jsonArraySaved = jsonArraySaved;       
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return jsonArraySaved.length();
}

@Override
public JSONObject getItem(int position) {
    // TODO Auto-generated method stub
    try {
        return jsonArraySaved.getJSONObject(position);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public static class ViewHolder {
    public GothicBoldTextView textViewFullName, textViewDistance;
    public GothicTextView textViewHeadLine, textViewTags;
    public ImageView imageViewPhoto;        
}

@Override
public View getView(final int position, View rawView, ViewGroup parent) {
    // TODO Auto-generated method stub      
    ViewHolder viewHolder;
    if (rawView == null) {
        rawView = LayoutInflater.from(context).inflate(
                R.layout.featured_events_list_item3, null);
        viewHolder = new ViewHolder();
        viewHolder.textViewFullName = (GothicBoldTextView) rawView
                .findViewById(R.id.textViewFullName);
        viewHolder.textViewHeadLine = (GothicTextView) rawView
                .findViewById(R.id.textViewHeadLine);
        viewHolder.textViewTags = (GothicTextView) rawView
                .findViewById(R.id.textViewTags);
        viewHolder.textViewDistance = (GothicBoldTextView) rawView
                .findViewById(R.id.textViewDistance);
        viewHolder.imageViewPhoto = (ImageView) rawView
                .findViewById(R.id.imageViewPhoto);
        rawView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rawView.getTag();
    }
    try {
        jsonObjectSaved = getItem(position);
        if (jsonObjectSaved.has("distance")) {
            distance = jsonObjectSaved.getString("distance");
            viewHolder.textViewDistance.setText(AccessClass
                    .getDistance(distance));
        }
        if (jsonObjectSaved.has("profile")) {
            jsonObjectSaved = jsonObjectSaved.getJSONObject("profile");
            if (jsonObjectSaved.has("firstname"))
                viewHolder.textViewFullName.setText(jsonObjectSaved
                        .getString("firstname"));
            if (jsonObjectSaved.has("lastname"))
                viewHolder.textViewFullName
                        .setText(viewHolder.textViewFullName.getText()
                                + " "
                                + jsonObjectSaved.getString("lastname"));
            if (jsonObjectSaved.has("headline"))
                viewHolder.textViewHeadLine.setText(jsonObjectSaved
                        .getString("headline"));
            if (jsonObjectSaved.has("tags")) {
                String tags = "";
                for (int i = 0; i < jsonObjectSaved.getJSONArray("tags")
                        .length(); i++) {
                    tags += jsonObjectSaved.getJSONArray("tags")
                            .getJSONObject(i).getString("name")
                            + ", ";
                }
                viewHolder.textViewTags.setText(tags.substring(0,
                        tags.length() - 2));
            }
            if (jsonObjectSaved.has("avatar_thumb")) {

                if (!jsonObjectSaved.getString("avatar_thumb").equals(
                        "/avatars/thumb/missing.png")) {
                    viewHolder.imageViewPhoto.setTag(jsonObjectSaved
                            .getString("avatar_thumb"));
                    SplashActivity.imageLoader.displayImage(context
                            .getResources().getString(R.string.domain)
                            + jsonObjectSaved.getString("avatar_thumb"),                                
                            viewHolder.imageViewPhoto);

                } else if (!jsonObjectSaved.isNull("picture_url")) {                            
                    viewHolder.imageViewPhoto.setTag(jsonObjectSaved
                            .getString("picture_url"));
                    SplashActivity.imageLoader.displayImage(
                            jsonObjectSaved.getString("picture_url"),                               
                            viewHolder.imageViewPhoto);
                }
            } else if (jsonObjectSaved.has("picture_url")
                    && !jsonObjectSaved.isNull("picture_url")) {                    
                viewHolder.imageViewPhoto.setTag(jsonObjectSaved
                        .getString("picture_url"));
                SplashActivity.imageLoader.displayImage(
                        jsonObjectSaved.getString("picture_url"),                           
                        viewHolder.imageViewPhoto);
            } else {                    
                viewHolder.imageViewPhoto.setTag("null");
                viewHolder.imageViewPhoto.setImageResource(R.drawable.photo2);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    rawView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                Intent intent;
                intent = new Intent(context, PublicProfileActivity.class);
                intent.putExtra("public_profile_id", ""
                        + getItem(position).getString("id"));
                context.startActivity(intent);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });
    rawView.invalidate();
    return rawView;
}
}
4

1 回答 1

0

我想你可能

adapter.imageLoader.clearCache();
adapter.imageLoader.stopThread();
listView.setAdapter(null);
于 2012-10-23T01:13:52.153 回答