【问题标题】:Loading contacts from phonebook causing the app to stop从电话簿加载联系人导致应用程序停止
【发布时间】:2015-08-17 02:38:41
【问题描述】:

我正在尝试从电话簿中获取所有联系人并将它们显示在 ListView 中。问题是联系人已被获取并在 ListView 中成功显示,但几秒钟后应用程序将崩溃,在 ListView 中获取联系人列表和拟合项目一切都成功,但应用程序在一段时间后崩溃。 这是 MainActivity.java-

   public class MainActivity extends ListActivity {
   // ArrayList<String> values = new ArrayList<String>();
String[] values;
Context mContext;
String Name;
int j=0,c=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
        Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
       c=c+1;
        }
        phones.close();
        values = new String[c];
       fun();
       MyArrayAdapter  adapter = new MyArrayAdapter(this, values);
        setListAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void fun() {
        // TODO Auto-generated method stub

        Cursor phones1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
         while (phones1.moveToNext())
            {
        Name=phones1.getString(phones1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String Number=phones1.getString(phones1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        values[j]=Name;
        j=j+1;
            }
         phones1.close();
    }
}

而 MyArrayAdapter.java 是 -

public class MyArrayAdapter extends ArrayAdapter<String> {
Context context;
String[] values;

public MyArrayAdapter(Context context, String[] values) {
        super(context, R.layout.activity_main, values);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.values = values;

    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.activity_main, parent, false);
        TextView txtv = (TextView) v.findViewById(R.id.member_name);
        TextView txtv1 = (TextView) v.findViewById(R.id.status);
        ImageView imgv = (ImageView)v.findViewById(R.id.profile_pic);
        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ok);
        for(int pos = 0; pos<values.length;pos++)
        {
            txtv.setText(values[position]);
            imgv.setImageBitmap(icon);

        }
                return v;
    }

}

请帮忙!

【问题讨论】:

  • 您在 logcat 上遇到的错误是什么?
  • 提供详细的错误代码,以便.people.can 帮助您。那么你得到了什么错误?
  • 对不起....但我看不到 logcat...

标签: android


【解决方案1】:

您应该加载联系人并将结果传递给后台服务中的适配器,例如AsyncTask、IntentService 作为在 UI Thread 上运行长时间的操作总是会使你的应用程序崩溃。

【讨论】:

  • 并为视图做延迟加载。
  • 您是否认为查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI 是“长时间运行的操作”?
  • 这不仅与查询有关,还与查询可能返回的内容有关。只返回姓名的查询不同于返回姓名、号码、电子邮件和图像的查询。
猜你喜欢
  • 2016-01-03
  • 2011-08-09
  • 2019-10-24
  • 2019-06-03
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多