我的问题是lv.SetAdapter会在此代码中导致强制关闭,但是当我在onCreate或...中使用它时它会起作用。我需要在JIFace之后设置Adapter - >打印流程,我该怎么办?谢谢 ! :)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
webView = new WebView(this);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(client);
webView.addJavascriptInterface(iface, "droid");
webView.loadUrl("http://google.com");
}
JIFace iface = new JIFace();
WebViewClient client = new MyClient();
class MyClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.loadUrl("javascript:window.droid.print(document.getElementsByTagName('body')[0].innerHTML);");
}
}
class JIFace {
public void print(String data) {
// here application do some processes and then creates an AdapterView with name aa
ListView lv = (ListView) findViewById(R.id.lv);
// the problem is it's not possible to setAdapter here... It's not important what's the ArrayAdapter...
lv.setAdapter(aa);
}
}
和我的LogCat:
01-17 11:21:33.990: E/AndroidRuntime(28906): FATAL EXCEPTION: WebViewCoreThread
01-17 11:21:33.990: E/AndroidRuntime(28906): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4056)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4109)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.view.View.invalidate(View.java:8621)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.view.View.invalidate(View.java:8572)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.widget.AbsListView.resetList(AbsListView.java:1716)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.widget.ListView.resetList(ListView.java:502)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.widget.ListView.setAdapter(ListView.java:442)
01-17 11:21:33.990: E/AndroidRuntime(28906): at com.example.MainActivity$JIFace.print(MainActivity.java:72)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:280)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.webkit.WebViewCore.loadUrl(WebViewCore.java:1840)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.webkit.WebViewCore.access$1700(WebViewCore.java:55)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1162)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.os.Looper.loop(Looper.java:137)
01-17 11:21:33.990: E/AndroidRuntime(28906): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:728)
01-17 11:21:33.990: E/AndroidRuntime(28906): at java.lang.Thread.run(Thread.java:856)
问题已解决:
我刚刚使用过:
Handler handler;
Runnable r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Handler = new H
r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// create ArrayAdapter here
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(aa);
}
};
webView = new WebView(this);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(client);
webView.addJavascriptInterface(iface, "droid");
webView.loadUrl("http://google.com");
}
JIFace iface = new JIFace();
WebViewClient client = new MyClient();
class MyClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.loadUrl("javascript:window.droid.print(document.getElementsByTagName('body')[0].innerHTML);");
}
}
class JIFace {
public void print(String data) {
// here application do some processes and then creates an AdapterView with name aa
ListView lv = (ListView) findViewById(R.id.lv);
// the problem is it's not possible to setAdapter here... It's not important what's the ArrayAdapter...
handler.post(r);
}
}