正如标题所述,当我执行此应用程序时,WebView将连接到“ https://null/” 插入了我放在EditText上的字符串,我无法找出问题所在
MainActivity活动
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活动
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();
}
}
答案 0 :(得分:1)
单击按钮时,您需要从EditText
而不是onCreate
中获取值。
此外,您没有使用相同的Intent
,因此实际上并没有传递该值。
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);
}
});
答案 1 :(得分:0)
您调用了错误的意图,并在下面的错误位置使用了gettext,您可以看到修改后的代码
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);
}
});
}
}
答案 2 :(得分:0)
您已将语句放置在“ onCreate()”方法中。将其放在“ Button.onClickListener”中。
答案 3 :(得分:0)
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);
}
});
}
}