我想從我的聯繫人列表中查看電話號碼和姓名。這是我迄今爲止:從聯繫人列表查看電話號碼和名稱ListView
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:試圖調用虛擬 法 '無效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(儀表的.java:1105) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368) 在android.app.ActivityThread.access $ 800(在Android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop()。 (Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5292) 在java.lang.reflect.Method.invoke(本地方法) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit的.java:908) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
我曾經打算去另一個Java類和佈局,並獲得額外並設置一個TextView到PhoneNumberStr。我相信這個字符串是空的。請幫幫我!
發佈oncreate方法和類/活動變量... –
@ΦXoce웃Пepeúpa我只是做了 – LUKER