2012-08-01 49 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); 
相关问题