我正在尝试将列表添加到 Spinner 但我总是在 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);
}
}