屏幕崩溃时崩溃

时间:2013-04-28 22:53:05

标签: android listview android-lifecycle baseadapter

我的应用程序运行良好,除非用户停止使用手机足够长的屏幕锁定,当他们解锁时,应用程序崩溃,我有点无能为力的原因。这是错误:

enter image description here

这是懒惰的适配器:

    package com.buhz.helpers;

import java.util.ArrayList;     
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.buhz.login.R;
import com.buhz.login.TabHostFragmentActivity;
import com.loopj.android.image.SmartImageView;



public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView name = (TextView)vi.findViewById(R.id.name); // title
        TextView message = (TextView)vi.findViewById(R.id.message); // artist name
        TextView created = (TextView)vi.findViewById(R.id.created); // duration
        SmartImageView thumb_image = (SmartImageView) vi.findViewById(R.id.list_image);



        HashMap<String, String> update = new HashMap<String, String>();
        update = data.get(position); 

        // Setting all values in listview
        name.setText(update.get("name"));
        message.setText(update.get("message"));
        created.setText(update.get("created"));
        thumb_image.setImageUrl(update.get("thumb_img"));
        name.setOnClickListener(new myOnClickListener(position));
        thumb_image.setOnClickListener(new myOnClickListener(position));
        return vi;
    }

    public class myOnClickListener implements OnClickListener{
        private int position;
        private String clicked_uid;
        public myOnClickListener(int position){
            this.position=position;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            HashMap<String, String> update = new HashMap<String, String>();
            update = data.get(position); 
            Log.i("Update Position:", update.toString());
            clicked_uid=update.get("uid");
            Log.d("Clicked UID:", clicked_uid+"");
            Intent i = new Intent(activity.getApplicationContext() , TabHostFragmentActivity.class);
            i.putExtra("profile_uid", clicked_uid);
            activity.startActivity(i);
            activity.finish();

        }

    }
}

2 个答案:

答案 0 :(得分:2)

正如the doc所说:

When a configuration change occurs at runtime, the activity is shut down 
and restarted by default, but declaring a configuration with this attribute
will prevent the activity from being restarted. 

所以我改变AndroidManifest.xml以包含所有配置。

android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|fontScale|uiMode|orientation|screenSize|smallestScreenSize|layoutDirection"

它对我有用。

答案 1 :(得分:0)

您必须确保处理所有可能的状态,以检查您的应用程序状态。当您的应用程序状态进入onResume或onStart等未处理状态时,会发生此问题。

本文将帮助您了解每个状态以及何时调用

http://www.android-app-market.com/android-activity-lifecycle.html