2011-06-17 60 views
0

我想添加列表爲微調,但我一直都想與在logcat的一個異常說:俊男項目添加到微調

"java.lang.RuntimeException: Unable to start activity ComponentInfo{....}: java.lang.NullPointerException" 

在模擬器中,一個對話框出現說該應用程序意外停止,我需要強制關閉。我嘗試過不同的事情,但我仍然得到相同的例外。

下面是活動的代碼:

public class CreateListActivity extends Activity implements OnClickListener{ 

/** Called when the activity is first created. */ 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    Spinner categorySpinner = (Spinner)findViewById(R.id.category_Spinner); 

    CategoryAction categoryAction = new CategoryAction(getBaseContext()); 
    ArrayList<ListCategory> categorylist = new ArrayList<ListCategory>(); 
    ArrayList<String> categoryNames = new ArrayList<String>(); 

    //Get all existing categories. 
    try 
    { 
     categorylist = (ArrayList<ListCategory>) categoryAction.getAllCategories(); 
    } 

    catch(SQLException e) 
    { 
     e.printStackTrace(); 
    }  

    // Add all existing category names. This will be used to add options to the spinner. 
    for (ListCategory category : categorylist) 
    { 
     categoryNames.add(category.getCategoryName()); 
    } 


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_spinner_item, categoryNames); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.createlist); 

    categorySpinner.setAdapter(adapter);  

    View addNewListButton = findViewById(R.id.Add_List_button); 
    addNewListButton.setOnClickListener(this); 
} 

public void onClick(View v) 
{ 
    ListAction listAction = new ListAction(getBaseContext()); 

    EditText listEditText = (EditText)findViewById(R.id.listName); 
    String newListName = listEditText.getText().toString(); 

    try { 
     if(!listAction.listExist(newListName)){ 
      listAction.createList(newListName, "To Buy"); 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    Intent viewListsIntent = new Intent(this, ItemListActivity.class); 
    startActivity(viewListsIntent); 
} 

}

回答

0

您的問題可能已經無關紡紗。在logcat中,向下滾動一下,指示出現空指針異常的那一行。然後修復那個空指針錯誤。

+0

非常感謝!我剛開始使用Android進行開發,但對IDE依然不熟悉。 – k4ru050 2011-06-17 00:55:03

0

您不能撥打findViewById之前您設置了查看內容。您必須移動這些行

super.onCreate(savedInstanceState); 
    setContentView(R.layout.createlist); 

到您的onCreate方法的頂部。您可能認爲它與Spinners有關,因爲您在這兩行之前添加了Spinner代碼。

相關問題