查看: 1225|回复: 2
|
Android startActivity Error
[复制链接]
|
|
package ro.tapi.www.ttouch;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
private static final String LOGTAG = "MainActivity";
private WebView Wv;
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Wv = (WebView)findViewById(R.id.Wv1);
Wv.getSettings().setJavaScriptEnabled(true);
Wv.addJavascriptInterface(new JsInteration(), "control");
Wv.loadUrl("http://www.google.com");
}
public class JsInteration {
Intent intent = new Intent("ro.tapi.www.ttouch.TreeActivity");
startActivity(intent); <<--- 为什么出现错误Invalid method declaration ; Return type requird .
}
}
|
|
|
|
|
|
|
|
发表于 29-4-2015 06:47 PM
|
显示全部楼层
本帖最后由 issac9413 于 29-4-2015 06:55 PM 编辑
First thing, this is wrong implementation of the class structure (JsInteration). Second thing, you cannot directly access the startActivity from external class.
- public class JsInteration {
- Intent intent = new Intent("ro.tapi.www.ttouch.TreeActivity");
- startActivity(intent); <<--- 为什么出现错误Invalid method declaration ; Return type requird .
- }
复制代码
Below is the correct way of creating the class. And you can call the control.start() from your javascript.
- public class MainActivity extends ActionBarActivity {
- private static final String LOGTAG = "MainActivity";
- private WebView Wv;
- @SuppressLint("JavascriptInterface")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Wv = (WebView)findViewById(R.id.Wv1);
- Wv.getSettings().setJavaScriptEnabled(true);
- Wv.addJavascriptInterface(new JsInteration(MainActivity.this), "control");
- Wv.loadUrl("http://www.google.com");
- }
- public class JsInteration {
- private Context _context;
- public JsInteration(Context context){
- this._context = context;
- }
- public void start(){
- Intent intent = new Intent("ro.tapi.www.ttouch.TreeActivity");
- _context.startActivity(intent);
- }
- }
复制代码
|
|
|
|
|
|
|
|

楼主 |
发表于 29-4-2015 07:58 PM
|
显示全部楼层
本帖最后由 munai.yi 于 19-5-2015 01:35 PM 编辑
我想
1 webview 打開 http://abc.com
2 當用戶按下Click 打開新的Activity
3 但總是沒打開新的Activity
JavaScript
<html>
</body>
<a href="#">Click</a>
<script language="javascript">
function g() {
window.control.x()
}
</script>
</html> Java
package ro.tapi.www.ttouch;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private static final String LOGTAG = "MainActivity";
private WebView Wv;
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Wv = (WebView)findViewById(R.id.Wv1);
Wv.getSettings().setJavaScriptEnabled(true);
Wv.addJavascriptInterface(new JsInteration(), "control");
Wv.loadUrl("http://abc.com");
Wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
});
}
public class JsInteration {
public void x(){
Intent intent = new Intent("ro.tapi.www.touch.Activity");
startActivity(intent);
}
}
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|