java 如何基于按钮单击在新屏幕上的 webview 中打开 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29752095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a url in webview on new screen based on button click
提问by srini vasan
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.
嗨,我已经在 stackoverflow 中尝试了所有可用的解决方案,但没有任何效果,单击按钮时应用程序停止。请帮帮我。
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.
每当我单击按钮时,应用程序就会崩溃。我希望应用程序在单击按钮时在 webview 中打开 url。请帮帮我,非常感谢任何帮助。
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);
}
}
-------------------------------
回答by Genevieve
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:
它崩溃的原因是因为您正在调用webview
属于另一个活动的 。因此,只需将一个字符串从您的活动传递到第二个活动,然后获取该字符串并将其加载到您的webview
. 以下是您如何实现它:
On the first activity, edit your onClick
method to:
在第一个活动中,将您的onClick
方法编辑为:
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;
}
}
then in your SecondActivity:
然后在你的 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);
}
}
回答by Tomer Shemesh
you might need to post you code here to see whats going on, but if your just trying to get a a webview popup you could just launch a browser activity to load that page
您可能需要在此处发布代码以查看发生了什么,但是如果您只是想获得 webview 弹出窗口,则可以启动浏览器活动来加载该页面
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
if you need it to be your activity you will need to pass an intent to your new activity and get the url out and then when you will just load it
如果您需要将其作为您的活动,您将需要将意图传递给您的新活动并获取网址,然后何时加载它
webView.loadUrl(URL);
if the issue is the crashing post the crashlog here so we can take a look what is going wrong
如果问题是崩溃,请在此处发布崩溃日志,以便我们查看发生了什么问题
回答by BSathvik
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);
String url;
switch(v.getId())
{
case R.id.button1:
url ="http://techcrunch.com/tag/rss/";
break;
case R.id.button2:
url = "http://www.bestchance.org.au/";
break;
default:
break;
}
startActivity(intent);
myWebView.loadUrl(url);
}
Just remove your code and paste this in.. you were trying to access the buttons that werent there because your started a new activity where the layout changed and there werent button to call them..
只需删除您的代码并将其粘贴到...