[英]Intent With Extras
我有一个上传活动,可以产生 3 个选项。 结果 1:数据上传成功。 结果 2:上传不成功。
上一个活动代码
protected void onPostExecute(String string1) {
super.onPostExecute(string1);
progressDialog.dismiss();
final Intent intent = new Intent(UploadActivity.this, SuccessActivity.class);
intent.putExtra("VAL1", string1);
startActivity(intent);
}
catch (Exception e) {
e.printStackTrace();
}
Mysql 结果代码
if(mysqli_query($con,$sql)){
//Data To Be Uploaded.........
echo "Your Data Was Successfully Uploaded. Thank You";
}
mysqli_close($con);
}else{ }
结果活动安卓
public class SuccessActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success_main);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String data = null;
String data2 = "Your Data Upload Was NOT Successfull!!";
if (getIntent()!= null && getIntent().getExtras() != null) {
if (extras.getString("val1") != null) {
data = extras.getString("val1");
textView = findViewById(R.id.experiences);
textView.setText(data);
}
else if(getIntent() != null && getIntent().getExtras() == null) {
textView = findViewById(R.id.experiences);
textView.setText(data2);
}
else if (getIntent() == null && getIntent().getExtras() == null){
textView = findViewById(R.id.experiences);
textView.setText(data2);
}
}
您额外的字符串“VAL1”不在捆绑包中。
将您的SuccessActivity
代码更改为此并尝试:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success_main);
TextView textView = findViewById(R.id.experiences);
String dataerror = "Your Data Upload Was NOT Successfull!!";
Intent intent = getIntent();
if(intent != null) {
String data = intent.getStringExtra("VAL1"); // Thats the right way to get the extra String;
if(data != null && !data.isEmpty()) {
textView.setText(data);
} else {
textView.setText(dataerror);
}
} else {
textView.setText(dataerror);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.