2012-04-30 73 views
0

我在android自動完成textview中遇到了一些麻煩。我用聯繫人姓名填充數組,但它仍然給我這個錯誤。什麼可能是錯誤的?Android自動完成空指針

下面是代碼:

  @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) { 

      } 
     }); 
} 

這裏是名稱的ArrayList的填充方式:

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); 日誌貓新的錯誤是 - >

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

它說你的活動的第41行。哪一行是第41行? –

+0

@AlexanderLucas這是第41行autoComplete.setThreshold(2); – Darko

+0

請包含您的整個onCreate()方法。 – Sam

回答

0

如果autoComplete.setThreshold(2);導致空指針,則聽起來好像你還沒有定義autoComplete.

您需要將其設置與此類似:

AutoCompleteTextView autoComplete = (AutoCompletetextView) findViewById(R.id.autoComplete); 
+0

是的,我忘了設置autocompletetextview,謝謝:) – Darko

1

變量autoComplete爲null。 setThreshhold和setAdapter拋出NPE異常,因爲你試圖訪問一個未被實例化的對象。

要進行測試,只檢查:

if(autoComplete == null) { 
    Log.e("MyApp", "autoComplete is null"); 
}