当前位置:  开发笔记 > 编程语言 > 正文

从MainActivity类中的另一个函数访问WebView

如何解决《从MainActivity类中的另一个函数访问WebView》经验,为你挑选了2个好方法。

如何正确访问onCreate函数中的"view"变量?我想要,loadUrl()但它会抛出一个错误,public Emitter.Listener onServerReceive = new Emitter.Listener() {view = loadUrl:

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException
    at com.myapp.MainActivity$7$1.run(MainActivity.java:399)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:177)
    at android.app.ActivityThread.main(ActivityThread.java:4947)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
    at dalvik.system.NativeStart.main(Native Method)
import com.github.nkzawa.emitter.Emitter;
....

public class MainActivity extends Activity {
    WebView view;
    Socket mSocket;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        view = (WebView) findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        ...
        view.loadUrl("file:///android_asset/loading.html");

    }

    ...

    @JavascriptInterface
    public void websocket_start(){
        try {
            mSocket = IO.socket("http://some web com:8888");
            mSocket.on("connect", onConnect);
            mSocket.on("server_receive", onServerReceive);
            mSocket.connect();
        } catch (URISyntaxException e) {}
    }


public Emitter.Listener onConnect = new Emitter.Listener() {
    @Override
    public void call(final Object... args) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonRootObject = new JSONObject();
                try {
                    jsonRootObject = new JSONObject("{ pn: " + global_login + ", pp: " + global_pass + " }");
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                mSocket.emit("Auth", jsonRootObject);
            }
        });
    }
};

    public Emitter.Listener onServerReceive = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    final String argsStr = TextUtils.join(",", args);
PROBLEM HERE "view" is null ->         view.loadUrl("javascript:receive('" + argsStr + "')");
                }
            });
        }
    };

}

我做错了什么,如何解决?谢谢!



1> Charles Durh..:

您在view初始化之前尝试使用.你需要等到view = (WebView) findViewById(R.id.webView);调用之后再进行初始化onServerReceive.

编辑:再看看这个更可能发生的是你正在使用你的实例onServerRecieve之前view的初始化onCreate().

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    view = (WebView) findViewById(R.id.webView);
    onServerRecieve = createListener()
    view.getSettings().setJavaScriptEnabled(true);
    ...
    view.loadUrl("file:///android_asset/loading.html");

}

...

@JavascriptInterface
public void websocket_start(){
    try {
        mSocket = IO.socket("http://some web com:8888");
        mSocket.on("connect", onConnect);
        mSocket.on("server_receive", onServerReceive);
        mSocket.connect();
    } catch (URISyntaxException e) {}
}

private Emitter.Listener createListener() {
    return new Emitter.Listener() {
    @Override
    public void call(final Object... args) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final String argsStr = TextUtils.join(",", args);
                view.loadUrl("javascript:receive('" + argsStr + "')");
            }
        });
    }
};
}

public Emitter.Listener onServerReceive;

}



2> waza123..:

终于找到了答案和问题

我有这个

view.addJavascriptInterface(new MainActivity(), "access_android");

在web js我用这个访问了android "access_android.websocket_emit()"

我认为因为"new MainActivity()"它创建了一个新的类,其中"视图"没有定义.所以我换成"new MainActivity()""this"

view.addJavascriptInterface(this, "access_android");

现在一切正常!谢谢你再见

推荐阅读
落单鸟人
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有