Java 在另一个Android活动类中使用自己的类

Java 在另一个Android活动类中使用自己的类,java,android,android-activity,Java,Android,Android Activity,我上过这门课: public class DisplayItems extends ListActivity{ ArrayList<String> contactNames = new ArrayList<>(); ArrayList<String> contactNumbers = new ArrayList<>(); //Get the contacts in the phone public void ge

我上过这门课:

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));
    }
}
但它不起作用。。。我是android studio的新手,当我的应用程序无法运行时,我甚至不知道在哪里查找错误消息
谢谢。

首先,您试图实例化扩展ListActivity的DisplayItems类,您不应该这样使用它。更好的方法是将DisplayItems转换为DisplayItemsFragment,如下所示

首先,确保您有一个id为container的布局

然后将其添加到PickNumbersActivity:

最后,让您的类像这样扩展ListFragment:

public class DisplayItemsFragment extends ListFragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        getContacts();

    }

    //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();
        displayContacts();
    }

    //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));
    }
}

如果您还有更多问题,请咨询官方。

如果您使用的是Android Studio,底部应该有一个标签,上面写着logcat。单击该按钮以获取错误消息。此外,当您运行应用程序时,它应该会自动弹出。还可以添加您自己的日志调试点。May Help单击DisplayItems,如果它未加载,则应该用红色下划线,然后按Alt+Enter键导入该类。如果不静态导入类,则无法引用它们。为什么要将一个活动包含到另一个活动中?这听起来像是一个糟糕的设计。谢谢你,我发现了它…很高兴知道,如果这个问题有帮助,那么你可能想通过勾选它旁边的复选标记来接受它。
if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new DisplayItemsFragment())
            .commit();
}
public class DisplayItemsFragment extends ListFragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        getContacts();

    }

    //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();
        displayContacts();
    }

    //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));
    }
}