Java WebView loadurl()-android应用程序
单击webview页面内的按钮时出错。当然,当我点击按钮时,它会变成谷歌网站。下面是代码和错误:- MainActivity.javaJava WebView loadurl()-android应用程序,java,android,webview,Java,Android,Webview,单击webview页面内的按钮时出错。当然,当我点击按钮时,它会变成谷歌网站。下面是代码和错误:- MainActivity.java package com.mt.nad.testwebapp; import android.content.Context; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.vi
package com.mt.nad.testwebapp;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private WebView webC;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webC = (WebView)findViewById(R.id.webView1);
webC.addJavascriptInterface(new JavaScriptInterface(), "CallJavaAdapter");
webC.setWebViewClient(new WebViewClient());
WebSettings webS = webC.getSettings();
webS.setJavaScriptEnabled(true);
webC.loadUrl("http://10.0.2.2/test-java-adapter/");
}
private class JavaScriptInterface{
JavaScriptInterface() {
}
@JavascriptInterface
public void gotoSite() {
//Toast.makeText(mContext, url, Toast.LENGTH_SHORT).show();
webC.clearCache(true);
webC.loadUrl("http://google.com");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
资料来源:
http://10.0.2.2/test-java-adapter/ (19)
是指
<input type="button" value="Go To Site" onClick="window.CallJavaAdapter.gotoSite()">
如果使用Toast或TextView,在单击按钮时可以更改,但对于WebView loadurl(),它不会加载
我指的是:但仍然没有运气…改变
@JavascriptInterface
public void gotoSite() {
//Toast.makeText(mContext, url, Toast.LENGTH_SHORT).show();
webC.clearCache(true);//Here you call the methond in other thread
webC.loadUrl("http://google.com");
}
致:
为什么白痴会对普通问题投否决票?如果您在代码中添加一些描述/注释,那么它将非常有用
http://10.0.2.2/test-java-adapter/ (19)
<input type="button" value="Go To Site" onClick="window.CallJavaAdapter.gotoSite()">
@JavascriptInterface
public void gotoSite() {
//Toast.makeText(mContext, url, Toast.LENGTH_SHORT).show();
webC.clearCache(true);//Here you call the methond in other thread
webC.loadUrl("http://google.com");
}
@JavascriptInterface
public void gotoSite() {
//Toast.makeText(mContext, url, Toast.LENGTH_SHORT).show();
new Handler().post(new Runnable() {
@Override
public void run() {
webC.clearCache(true);//Here you call the methond in UI thread
webC.loadUrl("http://google.com");
}
});
}
private void resetWebUrl(final String url){
runOnUiThread(new Runnable() {
@Override
public void run() {
webView.clearCache(true);//Here you call the methond in UI thread
webView.loadUrl("www.google.com");
}
});
}