【发布时间】:2016-04-22 16:49:33
【问题描述】:
我正在尝试从我的联系人列表视图中获取电话号码和姓名。这是我目前所拥有的:
ListView lv;
Cursor cursor1;
Context context;
String phoneNumberStr;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_screen_x);
context = this;
cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor1);
String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] to = {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor1, from, to);
setListAdapter(listAdapter);
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int PhoneNumber = cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNumberStr = cursor1.getString(PhoneNumber);
Intent SendText = new Intent(context, SendText.class);
SendText.putExtra("PhoneNumber", phoneNumberStr);
startActivity(SendText);
}
});
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
这是只获取数字,但我也想获取名称。
logcat 是这样说的:
原因:java.lang.NullPointerException:尝试调用虚拟 方法 'void android.widget.TextView.setText(java.lang.CharSequence)' 在空对象引用上 在 com.example.luke.percentagegradecalculator.SendText.onCreate(SendText.java:28) 在 android.app.Activity.performCreate(Activity.java:6020) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368) 在 android.app.ActivityThread.access$800(ActivityThread.java:149) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5292) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
我使用 Intent 转到另一个 java 类和布局并获取附加内容并将 textview 设置为 PhoneNumberStr。我相信这个字符串是空的。请帮帮我!
【问题讨论】:
-
发布 oncreate 方法和类/活动变量...
-
@ΦXoce웃Пepeúpa 我刚刚做了
标签: java android listview android-studio