Android中的活动突然崩溃

时间:2012-04-20 13:43:57

标签: android android-listview

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

主要是它在listview活动中崩溃了。但如果它在listview上使用数据加载崩溃,则必须提供异常。我还使用了thread,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;
}
}

1 个答案:

答案 0 :(得分:0)

我想你可能

adapter.imageLoader.clearCache();
adapter.imageLoader.stopThread();
listView.setAdapter(null);