捆绑传递出错

时间:2015-01-06 07:29:41

标签: java android

我有3个活动。在 product_dtls.java 我有两个按钮, add2cart 图片按钮。它打算在点击时添加2crt活动add2cart按钮或图像按钮。但是,add2cart按钮也将产品添加到数据库。 add2cart.java 有一个位置按钮,点击意图 buy_ltr.java ,其中我在edittexts中设置数据包。问题是,如果我通过点击add2cart按钮进入buy_ltr.java,编辑文本没有设置值。但是图像按钮点击工作正常。

product_dtls.java

public class Product_Details extends Activity{

TextView name,price,specification,feature;
String nme;
SQLiteDatabase mydb;
String pname;
String prprice;
String pspec;
String pfeature;
Button add2cart,by_nw;
ImageView image;
ImageButton imgbtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_dtls);
    image=(ImageView)findViewById(R.id.pr_img);
    name = (TextView) findViewById(R.id.txtPr_name);
    price = (TextView) findViewById(R.id.txtprice);
    specification=(TextView)findViewById(R.id.txtPr_spec);
    feature=(TextView)findViewById(R.id.txtPr_feature);
    imgbtn=(ImageButton)findViewById(R.id.cartimg);
    add2cart=(Button)findViewById(R.id.add2cart);
    by_nw=(Button)findViewById(R.id.buy_nw);

    Intent in = getIntent();
    Bundle bn = in.getExtras();
    Bundle bun=in.getExtras();
    final String dtl=bun.getString("key");
    nme = bn.getString("name");
    mydb = Product_Details.this.openOrCreateDatabase("products", MODE_PRIVATE, null);
    Cursor cr = mydb.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);
            Cursor cur=mydb.rawQuery("select * from add2cart where pnme='"+nm+"'",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 (pnme,prate)VALUES('"+nm+"','"+prprice+"')");
                Toast.makeText(getApplicationContext(),"add to cart",Toast.LENGTH_SHORT).show();
                Intent in=new Intent(Product_Details.this,add2cart.class);
                startActivity(in);

            }

        }
    });


    by_nw.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in=new Intent(Product_Details.this,buy_nw.class);
            Bundle bund = new Bundle();
            bund.putString("key",dtl);
            in.putExtras(bund);
            startActivity(in);
        }
    });
    imgbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent in=new Intent(Product_Details.this,add2cart.class);
            Bundle bundl = new Bundle();
            bundl.putString("dtls",dtl);
            in.putExtras(bundl);
            startActivity(in);
        }
    });

}

}

add2cart.java

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add2crt);
    adlstvw=(ListView)findViewById(R.id.lstvw_add2crt);
    btn=(Button)findViewById(R.id.place_order);

    //check bundle has null value

     if (bundl != null) {
     sme= bundl.getString("dtls");        
     }


    mydb=add2cart.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
    Cursor cr = mydb.rawQuery("SELECT * FROM add2cart", null);
    String [] pname = new String[cr.getCount()];
    String [] price = new String[cr.getCount()];

    int i = 0;
    while(cr.moveToNext())
    {
        String name = cr.getString(cr.getColumnIndex("pnme"));
        String prprice = cr.getString(cr.getColumnIndex("prate"));
        pname[i] = name;
        price[i] = prprice;
        i++;
    }
    CartAdapter cart=new CartAdapter(this,pname,price);
    adlstvw.setAdapter(cart);

    btn.setOnClickListener(new OnClickListener() {

         @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in=new Intent(add2cart.this,buy_ltr.class); 
            Bundle bndl = new Bundle();
            bndl.putString("som",sme); 
            in.putExtras(bndl);
            startActivity(in);

        }
    });

}

2 个答案:

答案 0 :(得分:0)

将此行移至ur oncreate ..

btn=(Button)findViewById(R.id.place_order);

也改变了这个

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    Intent in=new Intent(add2cart.this,buy_ltr.class);
    Bundle bundl = new Bundle();
    bundl.putString("dtls",sme);
    in.putExtras(bundl);
    startActivity(in);
}

修改

在add2cart

上注释这些行

这里你没有传递任何价值......

Intent in=new Intent(Product_Details.this,add2cart.class);
     startActivity(in);

这段代码的cos它登陆到没有值的add2cart活动所以你得到错误..

<强> EDIT2:

  by_nw.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             Intent in=new Intent(Product_Details.this,buy_nw.class);
            Bundle bund = new Bundle();
            bund.putString("key",dtl);
            in.putExtras(bund);
            startActivity(in);
        }
    });
    imgbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent in=new Intent(Product_Details.this,add2cart.class);
            Bundle bund = new Bundle();
            bund.putString("key",dtl);
            in.putExtras(bund);
            startActivity(in);
        }
    });

和你的add2cart

final String sme=bundl.getString("key");

答案 1 :(得分:0)

获得NullPointerException(首先是鸡蛋或鸡肉)是如此正常,您可以通过从捆绑包中获取字符串sme来指定dtls的值。活动:

final String sme=bundl.getString("dtls");

AND,您通过将dtls的值附加到Bundle,并通过获取sme的值来首先声明的值来指定dtls的值:

bundl.putString("dtls",sme);

所以,你刚才说:

int x, y;
x = y;
y = x;

那么,xy的价值是什么?它的 NULL

因此,为了克服这个问题,您必须在活动的第一次运行时add2cart.javasme分配另一个值,有很多方法,其中一种方法是创建{{1} }}变量来捕获活动的第一次运行:

static

测试public class add2cart extends Activity{ ListView adlstvw; Button btn,remove_btn; SQLiteDatabase mydb; public static boolean first_init = true; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.add2crt); adlstvw=(ListView)findViewById(R.id.lstvw_add2crt); btn=(Button)findViewById(R.id.place_order); if(first_init){ final String sme = "some_value"; first_init = false; }else{ Intent in=getIntent(); Bundle bundl=in.getExtras(); final String sme=bundl.getString("dtls"); } . . . } 按钮:

add2cart