gpt4 book ai didi

java - 安卓 : bundle passing

转载 作者:行者123 更新时间:2023-12-02 05:05:27 25 4
gpt4 key购买 nike

我必须将一个值从我的第一个 Activity 传递到第四个 Activity ...add2cart.java 是我的第三个 Activity ,buy_ltr.java 是我的第四个 Activity 。我想在第三个 Activity 中单击按钮时传递 bundle ,它应该进入第四个 Activity ,必须在其编辑文本字段中设置值。但是它不能正常工作。请帮助我。

每次模拟器因空点异常而崩溃时,我都会在add2cart.java中进行尝试

sme=bundl.getString("dtls");

我的尝试及其结果

1.传递值并在编辑文本中设置。但单击 add2cart 按钮时崩溃

  Intent in=getIntent();

Bundle bundl=in.getExtras();

sme=bundl.getString("dtls");

2. Intent 点击add2cart按钮进入下一页。但编辑文本字段为空。

  if(first_init){   
sme = "some_value";
first_init = false;
}

else{
Intent in=getIntent();
Bundle bundl=in.getExtras();
sme=bundl.getString("dtls");
}

3.传递值并在编辑文本中设置。但单击 add2cart 按钮时崩溃

 if(first_init){    
Intent in=getIntent();
Bundle bundl=in.getExtras();
sme=bundl.getString("dtls");
}
else{
sme = "some_value";
first_init = false;

}

add2cart.java

public class add2cart extends Activity{
ListView adlstvw;
Button btn,remove_btn;
SQLiteDatabase mydb;
@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);

Intent in=getIntent();
Bundle bundl=in.getExtras();
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);

}
});

}

}

Buy_ltr.java

public class buy_ltr extends Activity {
EditText edt1,edt2,edt3,edt4,edt5;
Button btn;
SQLiteDatabase mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.buy_ltr);
edt1=(EditText)findViewById(R.id.nameedit);
edt2=(EditText)findViewById(R.id.addrsedit);
edt3=(EditText)findViewById(R.id.cityedit);
edt4=(EditText)findViewById(R.id.pincodedit);
edt5=(EditText)findViewById(R.id.mobedit);
btn=(Button)findViewById(R.id.cnfm_ordr);


Intent in=getIntent();
Bundle bndl=in.getExtras();
String cont=bndl.getString("som");

mydb=this.openOrCreateDatabase("shopping", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS contacts(name varchar,adrs varchar,city varchar,pin varchar," +
"uname varchar,pass varchar,mob varchar,eid varchar)");

Cursor cur=mydb.rawQuery("SELECT * FROM contacts WHERE uname='"+cont+"' ", null);
while (cur.moveToNext()) {

String nm=cur.getString(cur.getColumnIndex("name"));
edt1.setText(nm);
String ad=cur.getString(cur.getColumnIndex("adrs"));
edt2.setText(ad);
String cty=cur.getString(cur.getColumnIndex("city"));
edt3.setText(cty);
String pc=cur.getString(cur.getColumnIndex("pin"));
edt4.setText(pc);


}
btn.setOnClickListener(new OnClickListener() {

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

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"my@email.com", "",};
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"name:"+edt1.getText().toString()+"\n"+"address:"+edt2.getText().toString()+"\n"+"city:"+edt3.getText().toString()+"\n"+"pin:"+edt4.getText().toString());

emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();

}
});
}

}

日志猫

01-07 23:06:57.093: E/AndroidRuntime(1428): FATAL EXCEPTION: main
01-07 23:06:57.093: E/AndroidRuntime(1428): Process: com.power.it.solar, PID: 1428
01-07 23:06:57.093: E/AndroidRuntime(1428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.power.it.solar/com.power.it.solar.add2cart}: java.lang.NullPointerException
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.os.Handler.dispatchMessage(Handler.java:102)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.os.Looper.loop(Looper.java:137)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-07 23:06:57.093: E/AndroidRuntime(1428): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 23:06:57.093: E/AndroidRuntime(1428): at java.lang.reflect.Method.invoke(Method.java:515)
01-07 23:06:57.093: E/AndroidRuntime(1428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-07 23:06:57.093: E/AndroidRuntime(1428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-07 23:06:57.093: E/AndroidRuntime(1428): at dalvik.system.NativeStart.main(Native Method)
01-07 23:06:57.093: E/AndroidRuntime(1428): Caused by: java.lang.NullPointerException
01-07 23:06:57.093: E/AndroidRuntime(1428): at com.power.it.solar.add2cart.onCreate(add2cart.java:35)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.app.Activity.performCreate(Activity.java:5243)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-07 23:06:57.093: E/AndroidRuntime(1428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)

最佳答案

从您发送的地方

 Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
intent.putExtra("EXTRA_ID", "SOME DATAS");
startActivity(intent);

到您收货的地方

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String datas= extras.getString("EXTRA_ID");
if (datas!= null) {
// do stuff
}
}

关于java - 安卓 : bundle passing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27833114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com