Reputation: 782
As title says, when i execute this app, WebView connects to "https://null/" insted of the string i put on the EditText, i cant figure out the problem
MainActivity activity
public class MainActivity extends AppCompatActivity {
EditText web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (EditText) findViewById(R.id.web);
String webstr = web.getText().toString();
Intent int2 = new Intent(MainActivity.this, WebView.class);
int2.putExtra("123", webstr);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), WebView.class);
v.getContext().startActivity(intent);
}
});
}
}
WebView Activity
public class WebView extends AppCompatActivity {
private android.webkit.WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Intent intent = getIntent();
String web1 = intent.getStringExtra("123");
webView = findViewById(R.id.webview);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://" + web1);
Toast.makeText(WebView.this, "Connected", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Views: 53
Reputation: 41
public class MainActivity extends AppCompatActivity {
EditText web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (EditText) findViewById(R.id.web);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String webstr = web.getText().toString();
Intent intent = new Intent(MainActivity.this, WebView.class);
intent.putExtra("123", webstr);
MainActivity.this.startActivity(intent);
}
});
}
}
Upvotes: 0
Reputation: 68
You have placed the statement in 'onCreate()' method. Place it inside the 'Button.onClickListener'.
Upvotes: 0
Reputation: 1852
You call wrong intent and with gettext at the wrong place here below you can see modified code
public class MainActivity extends AppCompatActivity {
EditText web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (EditText) findViewById(R.id.web);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String webstr = web.getText().toString();
Intent int2 = new Intent(MainActivity.this, WebView.class);
int2.putExtra("123", webstr);
v.getContext().startActivity(int2);
}
});
}
}
Upvotes: 0
Reputation: 5998
You need to get the value from your EditText
when you click the button, not within onCreate
.
Also you're not using the same Intent
, so you're not actually passing that value.
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), WebView.class);
String webstr = web.getText().toString();
intent.putExtra("123", webstr);
v.getContext().startActivity(intent);
}
});
Upvotes: 1