0

リストを 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);
}

}

4

2 に答える 2

0

あなたの問題はおそらくスピナーとは何の関係もありません。logcatで少し下にスクロールすると、nullポインター例外が発生した行が示されます。次に、そのnullポインタのバグを修正します。

于 2011-06-17T00:43:18.447 に答える
0

ビューの内容を設定するfindViewById 前に呼び出すことはできません。これらの行を移動する必要があります

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

メソッドの先頭にonCreate。この 2 行の前に Spinner コードを追加したため、おそらく Spinner に関係していると思われます。

于 2011-06-17T01:59:53.967 に答える