我做这个类使用自己的类:在另一个Android活动
public class DisplayItems extends ListActivity{
ArrayList<String> contactNames = new ArrayList<>();
ArrayList<String> contactNumbers = new ArrayList<>();
//Get the contacts in the phone
public void getContacts(){
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactNumbers.add(phoneNumber);
contactNames.add(name + "\n" + phoneNumber);
}
phones.close();
}
//displaying contacts name and numbers in a listView
public void displayContacts(){
ListView lstView = getListView();
lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
setListAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_checked, contactNames));
}
}
现在我想要使用这个类在这一个:
public class PickNumbersActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_numbers);
//Get message text
Intent i = getIntent();
Bundle extras = i.getExtras();
String message_text = extras.getString("message");
DisplayItems display = new DisplayItems();
display.getContacts();
display.displayContacts();
}
}
但它不工作...和我是新来的android工作室,我甚至不知道当我的应用程序不工作时,在哪里查找错误消息 谢谢。
如果您使用的是Android Studio,则应在底部标有“logcat”标签。点击它以获取错误信息。另外,它在运行应用程序时应该会自动弹出。 – IntegralOfTan 2015-02-06 18:03:51
也添加您自己的日志调试点。可以帮助 – 2015-02-06 18:04:55
点击“DisplayItems”(如果未加载,应该用红色加下划线),然后按Alt + Enter导入该类。如果没有静态导入它们,您将无法引用它们。 – aProperFox 2015-02-06 18:05:54