2012-08-01 50 views
1

我是Java/android的新手,所以很多這些術語都是外國的,但很樂意學習。我不會詳細討論應用程序,因爲我不認爲它是相關的。我的問題依然存在,我使用博客中的教程和代碼片段,並讓我的代碼工作。試圖清理和組織我的代碼當我移動一行時(創建我的autocompletetextview),我得到一個nullpoiner異常。以下是我用過的代碼。我的1號線的多數民衆贊成給我一個問題,碼android nullpointerexception函數

AutoCompleteTextView companyAutoComplete =(AutoCompleteTextView)addAddressDialog.findViewById(R.id.add_record_dialog_autocomplete);

當我在功能開始時將它移到正確的位置時,它出錯了,但是當它留在原地時它就像一個魅力。我想明白這是爲什麼。

public void addAddress() { 
    final Dialog addAddressDialog = new Dialog(this); 
    final int[] to = new int[] { android.R.id.text1 }; 
    final String[] from = new String[] { "CompanyName" }; 

    // Create a SimpleCursorAdapter for the CompanyName field. 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout. select_dialog_item, null, from, to); 

    addAddressDialog.setContentView(R.layout.add_record_dialog); 
    addAddressDialog.setTitle(getString(R.string.add_record_dialog_address_title)); 
    addAddressDialog.setCancelable(true); 

    final EditText text1 = (EditText) addAddressDialog.findViewById(R.id.add_record_dialog_edittext); 
    text1.setHint(getString(R.string.add_record_dialog_company_hint)); 

    Button buttonOK1 = (Button) addAddressDialog.findViewById(R.id.add_record_dialog_ok); 
    buttonOK1.setText(getString(R.string.add_record_dialog_ok_button)); 

    Button buttonCancel1 = (Button) addAddressDialog.findViewById(R.id.add_record_dialog_cancel); 
    buttonCancel1.setText(getString(R.string.add_record_dialog_cancel_button)); 

    buttonOK1.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v) { 
      Bundle addressBundle = new Bundle(); 
      addressBundle.putString("CompanyName", text1.getText().toString()); 

      Intent intent = new Intent(MenuActivity.this, AddAddressActivity.class); 
      intent.putExtras(addressBundle); 
      startActivity(intent); 

      addAddressDialog.dismiss(); 
     } 
    }); 

    buttonCancel1.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v) { 
      Toast.makeText(getBaseContext(), "Cancel button clicked", Toast.LENGTH_SHORT).show(); 
      addAddressDialog.dismiss(); 
     } 
    }); 
    AutoCompleteTextView companyAutoComplete = (AutoCompleteTextView) addAddressDialog.findViewById(R.id.add_record_dialog_autocomplete); 

    companyAutoComplete.setAdapter(adapter); 

    // Set an OnItemClickListener, to update dependent fields when 
    // a choice is made in the AutoCompleteTextView. 
    companyAutoComplete.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> listView, View view, 
        int position, long id) { 
      // Get the cursor, positioned to the corresponding row in the 
      // result set 
      Cursor cursor = (Cursor) listView.getItemAtPosition(position); 

      // Get the CompanyID from this row in the database. 
      String companyID = cursor.getString(cursor.getColumnIndexOrThrow("_id")); 

      // test to make sure CompanyID returned 
      Toast.makeText(getBaseContext(), companyID, Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    // Set the CursorToStringConverter, to provide the labels for the 
    // choices to be displayed in the AutoCompleteTextView. 
    adapter.setCursorToStringConverter(new CursorToStringConverter() { 
     public String convertToString(android.database.Cursor cursor) { 
      // Get the label for this row out of the "CompanyName" column 
      final int columnIndex = cursor.getColumnIndexOrThrow("CompanyName"); 
      final String str = cursor.getString(columnIndex); 
      return str; 
     } 
    }); 

    // Set the FilterQueryProvider, to run queries for choices 
    // that match the specified input. 
    adapter.setFilterQueryProvider(new FilterQueryProvider() { 
     public Cursor runQuery(CharSequence constraint) { 
      Cursor cursorReturn = dbAdapter.getCompanies(constraint != null ? constraint.toString() : null); 

      startManagingCursor(cursorReturn); 
      return cursorReturn; 
     } 
     }); 

    addAddressDialog.show(); 
} 

回答

2

發生這種情況是因爲您稍後致電setContentView

setContentView設置addAddressDialog對話框的佈局。如果您不打電話setContentView,它沒有佈局項目,因此addAddressDialog.findViewById(...);將爲null,顯然,您不能將其轉換爲任何內容,也不能對其調用setHint

它應該沒有關係,這行代碼在你的方法中,只要你的行setContentView被稱爲之前它。

+0

嘛。謝謝你的信息。我很習慣VB,不需要擔心UI。 – Huascar 2012-08-01 23:33:31

0

唯一重要的事情是,你的findViewById()呼叫在呼叫setContentView()後調用,即該行:

addAddressDialog.setContentView(R.layout.add_record_dialog); 

XML文件add_record_dialog.xml是,你遍歷查找與視圖查看層次add_record_dialog_autocomplete的ID。直到你給出對話框的視圖層次結構,它不能遍歷它,因此當你嘗試使用AutoCompleteTextView時,你會得到一個NullPointerException,因爲它找不到你的視圖。

編輯:另外,如果你的意思是你把它放在了方法的一開始,這也將失敗歸因於有意義的事實,addAddressDialog將是空的,直到您的來電

final Dialog addAddressDialog = new Dialog(this); 
相關問題