Android : 包传递
Android : bundle passing
我必须将第一个 activity 的值传递给第四个 activity...add2cart.java 是我的第三个 activity,buy_ltr.java 是我的第四个 activity.I 想要在第三个 activity 中通过单击按钮传递捆绑包,它应该在第四个 activity 中获得并且必须在其编辑文本中设置值 field.But 它不是工作 fine.Please 帮助我。
我在 add2cart.java 中进行了尝试。每次模拟器因 行中的空点异常 而崩溃时
sme=bundl.getString("dtls");
我的尝试及其结果
1.Pass 值并在编辑中设置 text.But 单击 add2cart 按钮时崩溃
Intent in=getIntent();
Bundle bundl=in.getExtras();
sme=bundl.getString("dtls");
2.Intent 点击 add2cart 进入下一页 button.But 编辑文本字段为空白。
if(first_init){
sme = "some_value";
first_init = false;
}
else{
Intent in=getIntent();
Bundle bundl=in.getExtras();
sme=bundl.getString("dtls");
}
3.Pass 值并在编辑中设置 text.But 单击 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.access0(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
}
}
如果您使用 startActity() 或 startActivityForResult 启动 activity 并将 Bundle 作为额外的,则 Bundle 将传递给另一个 activity。
在从包中获取数据之前检查您是否正在获取包(包不为空)。
我必须将第一个 activity 的值传递给第四个 activity...add2cart.java 是我的第三个 activity,buy_ltr.java 是我的第四个 activity.I 想要在第三个 activity 中通过单击按钮传递捆绑包,它应该在第四个 activity 中获得并且必须在其编辑文本中设置值 field.But 它不是工作 fine.Please 帮助我。
我在 add2cart.java 中进行了尝试。每次模拟器因 行中的空点异常 而崩溃时
sme=bundl.getString("dtls");
我的尝试及其结果
1.Pass 值并在编辑中设置 text.But 单击 add2cart 按钮时崩溃
Intent in=getIntent();
Bundle bundl=in.getExtras();
sme=bundl.getString("dtls");
2.Intent 点击 add2cart 进入下一页 button.But 编辑文本字段为空白。
if(first_init){
sme = "some_value";
first_init = false;
}
else{
Intent in=getIntent();
Bundle bundl=in.getExtras();
sme=bundl.getString("dtls");
}
3.Pass 值并在编辑中设置 text.But 单击 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.access0(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
}
}
如果您使用 startActity() 或 startActivityForResult 启动 activity 并将 Bundle 作为额外的,则 Bundle 将传递给另一个 activity。 在从包中获取数据之前检查您是否正在获取包(包不为空)。