如何基于按钮单击在新屏幕上打开webview中的URL [英] How to open a url in webview on new screen based on button click
问题描述
您好我已经尝试了stackoverflow中的所有可用解决方案,但没有任何工作,单击按钮时应用程序停止。请帮帮我。
Hi i have tried all available solutions in stackoverflow but nothing is working, app is stopped when the button is clicked. Please help me out.
每当我点击按钮,应用程序崩溃。我希望应用程序在单击按钮时打开webview中的URL。请帮助我,非常感谢任何帮助。
Whenever i click the button, the app crashes. I want the app to open the url in a webview when the button is clicked. Please help me out, any help is greatly appreciated.
任何人都可以发布修改后的代码。
Can anyone please post the modified code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:text="@string/button01"
android:onClick="onClick"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:text="@string/button02"
android:onClick="onClick"/>
</LinearLayout>
------------------------------
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
-----------------------------------------------------
mainactivity.java
package com.example.webview;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v)
{
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
final Context context = this;
Intent intent = new Intent(context, SecondActivity.class);
startActivity(intent);
switch(v.getId())
{
case R.id.button1:
myWebView.loadUrl("http://techcrunch.com/tag/rss/");
break;
case R.id.button2:
myWebView.loadUrl("http://www.bestchance.org.au/");
break;
default:
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
--------------------
secondactivity.java
package com.example.webview;
import android.webkit.WebView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SecondActivity extends ActionBarActivity {
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
}
}
-------------------------------
推荐答案
崩溃的原因是因为你正在调用属于另一个活动的 webview
。因此,只需将活动中的字符串传递给第二个活动,然后获取字符串并将其加载到 webview
。以下是如何实现它:
The reason why it's crashing is because you are calling the webview
which belongs to another activity. So instead, just pass a string from your activity to second activity then get the string and load it to your webview
. Below is how you can achieve it:
在第一个活动中,编辑 onClick
方法:
On the first activity, edit your onClick
method to:
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
switch(v.getId())
{
case R.id.action_bar:
intent.putExtra("url", "http://techcrunch.com/tag/rss/");
startActivity(intent);
break;
case R.id.action_bar_spinner:
intent.putExtra("url", "http://www.bestchance.org.au/");
startActivity(intent);
break;
default:
break;
}
}
然后在你的SecondActivity中:
then in your SecondActivity:
public class SecondActivity extends ActionBarActivity {
private WebView myWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
String url = getIntent().getStringExtra("url");
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
}
这篇关于如何基于按钮单击在新屏幕上打开webview中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!