Passing Intent can't get extra();

2019-02-21 00:47发布

问题:

Hi i can't get logged in already in a web page at mine main activity i passed inten to another activity but i can't get an extract. How can i solve it

this is mine main activity public class ViewActivity extends Activity {

private EditText mTextUserName;
private EditText mTextPassword;
public String user_name;
public String pass_word;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextUserName = (EditText) findViewById(R.id.textUserName);
    mTextPassword = (EditText) findViewById(R.id.textPassword);



    final Button mButtonLogin = (Button) findViewById(R.id.buttonLogin);
    mButtonLogin.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            user_name = mTextUserName.getText().toString();
            pass_word = mTextPassword.getText().toString();

            Intent goToNextActivity = new Intent(getApplicationContext(), ViewActivity.class);
            goToNextActivity.putExtra("username", user_name);
            goToNextActivity.putExtra("username", pass_word);

            startActivity(goToNextActivity);


        }
    });

and here view activity

private WebView webView;

public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);
    webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setDefaultFontSize(20);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new MyWebViewClient());




    webView.loadUrl("http://something.com");



}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
        goToNextActivity.getExtras();
        return true;

    }
}

it is loads a web page but not logged in

回答1:

Intent extras = getIntent().getExtras();
String newString;
if(extras == null) {
    newString= null;
} else {
    newString= extras.getString("username");
}

and for two values both username and password you are giving same key values...please change and try it.



回答2:

Use just below code for getting data from intent:-

Intent intent = getIntent();
String username= intent.getStringExtra("username");

Write above code in shouldOverrideUrlLoading method.



回答3:

Here

Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);

you are constructing a brand new Intent which is not what you want. You want to use the Intent that started the Activity.

For this reason, you want to use

Intent i = getIntent();

because we can see from the Activity Docs that getIntent()

Return the intent that started this activity.

then get the extras if they are not null

if (i.getExtras() != null) {
    String userName = i.getStringExtra("username");
    String password = i.getStringExtra("password");  // you have a typo in your passing Intent
}

You need your Activity Context for this so it is better/easier to do it in onCreate() instead of your inner-class unless there is a reason you can't do it there.



回答4:

Try this way,hope this will help you to solve your problem.

There is two way pass/get data one activity to another activity.

1.add data to intent.

how to put :

Intent goToNextActivity = new Intent();
goToNextActivity.putExtra("username", user_name);
goToNextActivity.putExtra("password", pass_word);

how to get :

String username = getIntent().getStringExtra("username");
String password = getIntent().getStringExtra("password");

2.Add data to bundle and add bundle to intent.

how to put :

Intent goToNextActivity = new Intent();
Bundle bundle = new Bundle();
bundle.putString("username", user_name);
bundle.putString("password", pass_word);
goToNextActivity.putExtras(bundle);

how to get :

String username = getIntent().getExtras().getString("username");
String password = getIntent().getExtras().getString("password");