-1

按照WebView文档,我想注入一个公共 java 函数webView并使用 JavasSript 调用它,当该函数的形式为

public String myfunc(){ 
//....
}

它可以工作,但是在使用throws关键字时会导致致命错误。

这是我的完整代码:

package com.arad.test1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.Toast;

import com.arad.test1.model.test;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test1Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test);
        final WebView myWebView = (WebView) findViewById(R.id.webView1);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setPluginState(PluginState.ON);
        myWebView.addJavascriptInterface(this, "appConnector");
        try {
            myWebView.loadDataWithBaseURL(
                    "file:///android_asset/",
                    convertStreamToString(getAssets().open(
                            "templates/test.html")), "text/html", "utf-8", "");
        } catch (IOException e) {
            Log.e("Test1", e.getMessage());
        }
    }

    public String load() {
        return generateJsonFromClass();
    }

    private String generateJsonFromClass() {
        try {
            ObjectMapper mapper = new ObjectMapper();
            test t = new test();
            String result = mapper.writeValueAsString(t);
            return result;
        } catch (JsonGenerationException e) {
            Log.e("Test1", e.getMessage());
            return "JsonGenerationException";
        } catch (JsonMappingException e) {
            Log.e("Test1", e.getMessage());
            return "JsonMappingException";
        } catch (IOException e) {
            Log.e("Test1", e.getMessage());
            return "IOException";
        }
    }

    private static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

这是 DDMS 日志:

07-10 10:02:42.183: E/dalvikvm(20521): Could not find class 'com.fasterxml.jackson.databind.ObjectMapper', referenced from method com.arad.test1.Test1Activity.generateJsonFromClass 
07-10 10:02:42.183: W/dalvikvm(20521): VFY: unable to resolve new-instance 28 (Lcom/fasterxml/jackson/databind/ObjectMapper;) in Lcom/arad/test1/Test1Activity; 
07-10 10:02:42.183: D/dalvikvm(20521): VFY: replacing opcode 0x22 at 0x0000 
07-10 10:02:42.183: W/dalvikvm(20521): VFY: unable to resolve exception class 26 (Lcom/fasterxml/jackson/core/JsonGenerationException;) 
07-10 10:02:42.183: W/dalvikvm(20521): VFY: unable to find exception handler at addr 0xf 
07-10 10:02:42.183: W/dalvikvm(20521): VFY: rejected Lcom/arad/test1/Test1Activity;.generateJsonFromClass ()Ljava/lang/String; 
07-10 10:02:42.183: W/dalvikvm(20521): VFY: rejecting opcode 0x0d at 0x000f 
07-10 10:02:42.183: W/dalvikvm(20521): VFY: rejected Lcom/arad/test1/Test1Activity;.generateJsonFromClass ()Ljava/lang/String; 
07-10 10:02:42.212: W/dalvikvm(20521): Verifier rejected class Lcom/arad/test1/Test1Activity; 
07-10 10:02:42.212: W/dalvikvm(20521): Class init failed in newInstance call (Lcom/arad/test1/Test1Activity;) 
07-10 10:02:42.223: D/AndroidRuntime(20521): Shutting down VM 
07-10 10:02:42.223: W/dalvikvm(20521): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
07-10 10:02:42.322: E/AndroidRuntime(20521): FATAL EXCEPTION: main 
07-10 10:02:42.322: E/AndroidRuntime(20521): java.lang.VerifyError: com/arad/test1/Test1Activity 
07-10 10:02:42.322: E/AndroidRuntime(20521): at java.lang.Class.newInstanceImpl(Native Method) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at java.lang.Class.newInstance(Class.java:1319) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at android.os.Looper.loop(Looper.java:137) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at android.app.ActivityThread.main(ActivityThread.java:4424) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at java.lang.reflect.Method.invokeNative(Native Method) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at java.lang.reflect.Method.invoke(Method.java:511) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
07-10 10:02:42.322: E/AndroidRuntime(20521): at dalvik.system.NativeStart.main(Native Method) 
07-10 10:02:42.352: I/dalvikvm(20521): threadid=3: reacting to signal 3 
07-10 10:02:42.432: I/dalvikvm(20521): Wrote stack traces to '/data/anr/traces.txt' 
07-10 10:02:42.632: I/dalvikvm(20521): threadid=3: reacting to signal 3 
07-10 10:02:42.652: I/dalvikvm(20521): Wrote stack traces to '/data/anr/traces.txt' 
07-10 10:02:43.122: I/dalvikvm(20521): threadid=3: reacting to signal 3 
07-10 10:02:43.213: I/dalvikvm(20521): Wrote stack traces to '/data/anr/traces.txt' 

如何解决这个问题?

4

1 回答 1

0

将您的库文件夹名称从 lib 更改为 libs

Android:jar 文件(LIB 或 LIBS)的文件夹名称是什么?

于 2012-07-10T05:04:57.423 回答