0

I am trying to get data from editText box and open that url on Button click. For that I have to send data from one intent to another. For that, I have referred this and this but was unable to send data.. I am trying to send data from main to web view activity..

MainActivity.Java

public class MainActivity extends Activity {
Button b;

WebView mWebVIew2;
EditText mEdit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b = (Button) findViewById(R.id.button1);
    mEdit = (EditText) findViewById(R.id.editText1);
    final String s = mEdit.getText().toString();

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), mEdit.getText(), Toast.LENGTH_LONG).show();
            Intent i = new Intent(getApplicationContext(), WebViewActivity.class);
            i.putExtra("mSite", s);
            startActivityForResult(i, 0);
        }
    });

}

}

WebViewActivity.java

public class WebViewActivity extends Activity {
private WebView mWebView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webv);
    mWebView = (WebView) findViewById(R.id.webView1);
    Intent mIntent = getIntent();
    String y = mIntent.getExtras().getString("mSite");

    Toast.makeText(getApplicationContext(), y, Toast.LENGTH_LONG).show();

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl(y);

}

}
Community
  • 1
  • 1
Aexyn
  • 1,212
  • 2
  • 13
  • 30

1 Answers1

1

You String s is defined as mEdit.getText in the onCreate. It does not reflect the content of the editText when you click your button.

Instead, fetch the value for s in the onClick method.

edit

In further details : in Java, String objects are immutable. A pointer may point to a different reference, but a reference may not be altered (i.e. muted).

In this case, that means that your String s can never reflect the value of the EditText, but only a copy at a given time. In this particular scenario, the String s contains the content of the editText at startup (in onCreate), which most likely contains either nothing or a default value.

njzk2
  • 38,969
  • 7
  • 69
  • 107