简体   繁体   中英

Intent With Extras

I have an upload activity that can result to 3 options. Result 1: Successful Upload of data. Result 2: Upload not Successful.

Previous Activity Code

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 Results code

if(mysqli_query($con,$sql)){
       //Data To Be Uploaded.........
        echo "Your Data Was Successfully Uploaded. Thank You";
    }
    mysqli_close($con);
}else{ }

ResultsActivity Android

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);
            }
        }

Referring to this: How do I get extra data from intent on Android?

Your extra String "VAL1" is not in a Bundle.

Change your SuccessActivity code to this and try:

@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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM