2012-03-22 38 views
0

Thsi的的setText是我的代碼空指針:當的EditText

EditText editCategoryName, editBrandName; 
    private ArrayAdapter<String> myAdapter1, myAdapter2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
//  mylist = new ArrayList<HashMap<String, String>>(); 
//  mylist2 = new ArrayList<HashMap<String, String>>(); 
     myList1 = new ArrayList<String>(); 
     myList2 = new ArrayList<String>(); 
     editCategoryName = (EditText)findViewById(R.id.editCategoryName); 
     editBrandName = (EditText)findViewById(R.id.editBrandName); 
     editCategoryName.setText("Google is your friend.", TextView.BufferType.EDITABLE); 

<EditText 
     android:id="@+id/editCategoryName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="請輸入分類名稱" 
     android:singleLine="true" > 
    </EditText> 

    <TextView 
     android:id="@+id/textSpinnerBrand" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="hint" /> 

    <Spinner 
     android:id="@+id/spinnerBrand" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <EditText 
     android:id="@+id/editBrandName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="hint" 
     android:singleLine="true" > 
    </EditText> 

空指針錯誤顯示,在行:

editCategoryName.setText("Google is your friend.", TextView.BufferType.EDITABLE); 

我不知道爲什麼。請幫助..

+0

'setContentView(...);' – 2012-03-22 05:19:44

回答

1

super.onCreate(savedInstanceState); 此行後,你忘了setContentView(R.layout.<xml file name>)

5

你沒有在onCreate方法定義setContetnView(R.layout.xyz);

1

你忘了定義setContentView(R.layout.you_xml_name);在onCreate方法中。

EditText editCategoryName, editBrandName; 
private ArrayAdapter<String> myAdapter1, myAdapter2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    setContentView(R.layout.you_xml_name); 
    super.onCreate(savedInstanceState); 
    myList1 = new ArrayList<String>(); 
    myList2 = new ArrayList<String>(); 
    editCategoryName = (EditText)findViewById(R.id.editCategoryName); 
    editBrandName = (EditText)findViewById(R.id.editBrandName); 
    editCategoryName.setText("Google is your friend.", TextView.BufferType.EDITABLE); 
1

給定setContentView(「佈局文件的ID」);然後在xml文件中使用編輯文本和旋鈕的ID。否則,它會顯示空指針,因爲您試圖使用一個id,它是您尚未使用的XML佈局的一部分。

相關問題