0

我在 android 中的自动完成 textview 遇到了一些问题。我用联系人姓名填充数组,但它仍然给我这个错误。有什么问题?

这是代码:

           @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        contactArr = new ArrayList<Contacts>();
        names=new ArrayList<String>();
        t = (TextView) findViewById(R.id.textView1);
        et = (EditText) findViewById(R.id.editText1);
        button = (Button) findViewById(R.id.button1);
        test();
        Log.v("test", names.get(3));
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_item, names);
//      autoComplete.setThreshold(2);
        autoComplete.setAdapter(adapter);
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

            }
        });
}

以下是名称数组列表的填充方式:

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));
            names.add(name); //populated here

最后是来自 logcat 的错误:

04-30 19:26:15.389: E/AndroidRuntime(25176): FATAL EXCEPTION: main
04-30 19:26:15.389: E/AndroidRuntime(25176): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.contact/com.contact.ContactInfoActivity}: java.lang.NullPointerException
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.os.Looper.loop(Looper.java:130)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.app.ActivityThread.main(ActivityThread.java:3835)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at java.lang.reflect.Method.invoke(Method.java:507)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at dalvik.system.NativeStart.main(Native Method)
04-30 19:26:15.389: E/AndroidRuntime(25176): Caused by: java.lang.NullPointerException
04-30 19:26:15.389: E/AndroidRuntime(25176):    at com.contact.ContactInfoActivity.onCreate(ContactInfoActivity.java:41)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 19:26:15.389: E/AndroidRuntime(25176):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-30 19:26:15.389: E/AndroidRuntime(25176):    ... 11 more

当我删除此行时 autoComplete.setThreshold(2); log cat 中的新错误是->

04-30 19:37:10.319: E/AndroidRuntime(25564):    at com.contact.ContactInfoActivity.onCreate(ContactInfoActivity.java:42)
4

2 回答 2

1

变量 autoComplete 为空。setThreshhold 和 setAdapter 正在引发 NPE 异常,因为您正在尝试访问尚未实例化的对象。

要对此进行测试,只需检查:

if(autoComplete == null) {
    Log.e("MyApp", "autoComplete is null");
}
于 2012-04-30T17:41:10.690 回答
0

如果autoComplete.setThreshold(2);导致一个空指针,那么听起来你还没有定义autoComplete.

您需要像这样设置它:

AutoCompleteTextView autoComplete = (AutoCompletetextView) findViewById(R.id.autoComplete);
于 2012-04-30T17:40:39.020 回答