setText中的Android-Null指针异常

时间:2015-02-02 06:16:18

标签: android nullpointerexception

我有一个SQLite数据库来存储购物车中的商品。每次用户点击addtocart按钮时,我需要在购物车中对应每个用户的产品数量并将其设置为textview。但是我的代码会抛出一个零点异常。请帮助我。

我在第109行得到了nullpointer异常: if(!crtno.getText()。toString()。equals(“”))

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_dtls);
    crtno=(TextView)findViewById(R.id.crtno);
    imgbtn=(ImageButton)findViewById(R.id.cartimg);
    add2cart=(Button)findViewById(R.id.add2cart);

    DataBaseHandler dbh = new DataBaseHandler(this);
    SQLiteDatabase db = dbh.getWritableDatabase();

    Intent in = getIntent();
    Bundle bn = in.getExtras();
    Bundle bun=in.getExtras();
    final String dtl=bun.getString("key");
    nme = bn.getString("name");

    Cursor cr = db.rawQuery("SELECT * FROM product WHERE pname = '"+nme+"'", null);

    while(cr.moveToNext())
    {
        String name = cr.getString(cr.getColumnIndex("pname"));
        String pr1price = cr.getString(cr.getColumnIndex("pprice"));
        String prspc=cr.getString(cr.getColumnIndex("pspec"));
        String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
        pname = name;
        prprice = pr1price;
        pspec=prspc;
        pfeature=prfeature;
    }
    name.setText(pname);
    price.setText("Rs " +prprice + "/-");
    specification.setText(pspec);
    feature.setText(pfeature);


    add2cart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            boolean incart=false;
            String nm=name.getText().toString();

            mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
            mydb.execSQL("CREATE TABLE IF NOT EXISTS add2cart(usr TEXT,img BLOB,pnme TEXT,prate NUMERIC,pqty NUMERIC,ptotl NUMERIC)");
            Cursor cur=mydb.rawQuery("select * from add2cart where pnme='"+nm+"' AND usr='"+dtl+"'",null);

            if (cur.moveToFirst()){
                String prdname=cur.getString(cur.getColumnIndex("pnme"));

                if (nm.equals(prdname)){
                    add2cart.setText("Already in Cart");
                    incart=true;
                }
            }

            if(incart==false){
                mydb.execSQL("INSERT INTO add2cart (usr,pnme,prate)VALUES('"+dtl+"','"+nm+"','"+prprice+"')");
                Toast.makeText(getApplicationContext(),"added to cart",Toast.LENGTH_SHORT).show();                  Cursor crsr=mydb.rawQuery("select pnme from add2cart where usr='"+dtl+"'", null);

                int count=0;
                if (!crtno.getText().toString().equals("")) {

                    count=crsr.getCount();                      
                }
                crtno.setText(Integer.toString(count));

            }

        }
    });
}
}

2 个答案:

答案 0 :(得分:1)

尝试

crtno.getText().toString().equals("")

而不是

crtno.equals("")

因为crtno.getText().toString().equals("")是检查textview中文本相等性的命令。

答案 1 :(得分:0)

首先,您应首先拉出textview中显示的字符串,并按以下方式完成:

getText()

Return the text the TextView is displaying. 

所以你的方式应该是

crtno.getText().toString().equals("")

也试试这个

crtno.getText().toString().equals(null)