【发布时间】: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