NullPointerExceptions,无法想出这个

时间:2014-03-20 21:25:16

标签: java android exception

代码:

public class ProductsActivity extends Activity {

DbHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_products);

    db = new DbHelper(getApplicationContext());
    Intent intent = getIntent();
    String listName = intent.getExtras().getString("position");

    List<Products> listOfProducts = db.getAllProductsByList(listName);
    List<String> stringListOfProducts = new ArrayList<String>();

    for (int i = 0; i < listOfProducts.size(); i++)
        stringListOfProducts.add(listOfProducts.get(i).getProductName());

    ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, stringListOfProducts);

    ListView lv = (ListView) findViewById(R.id.listView2);

    lv.setAdapter(myArrayAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.products, menu);
    return true;
    }

  }

错误:

我在第37行收到错误

  

lv.setAdapter(myArrayAdapter);

我有另一个项目,它完成所有工作,并且工作正常!

0 个答案:

没有答案