How correctly get access to "view" variable in onCreate
function ? I want loadUrl()
but it throws an error on line with, public Emitter.Listener onServerReceive = new Emitter.Listener() {
on view = loadUrl
:
如何正确访问onCreate函数中的“view”变量?我想要loadUrl(),但它会抛出一个错误,公共Emitter.Listener onServerReceive = new Emitter.Listener(){on 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 + "')");
}
});
}
};
}
What do I do wrong, how to fix it ? thanks!
我做错了什么,如何解决?谢谢!
2 个解决方案
#1
15
You're trying to use view
before it has been initialized. You need to wait until after view = (WebView) findViewById(R.id.webView);
has been called in order to initialize onServerReceive
.
您正在尝试在初始化之前使用视图。你需要等到view =(WebView)findViewById(R.id.webView)之后;已被调用以初始化onServerReceive。
EDIT: Looking at this some more what's more likely happening is that you're using your instance of onServerRecieve
before view
is initialized in onCreate()
.
编辑:再看看这个更可能发生的事情是你在onCreate()中初始化视图之前使用你的onServerRecieve实例。
@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
13
Finally found the answer and the problem
终于找到了答案和问题
I had this
我有这个
view.addJavascriptInterface(new MainActivity(), "access_android");
and in web js I accessed android with this "access_android.websocket_emit()"
在web js我用这个“access_android.websocket_emit()”访问了android
and i think because of "new MainActivity()"
it was creating a new class where "view" wasn't defined. so I replaced "new MainActivity()"
with "this"
我认为,由于“新的MainActivity()”,它正在创建一个新的类,其中“视图”未定义。所以我用“this”替换了“new MainActivity()”
view.addJavascriptInterface(this, "access_android");
now all works fine ! thanks good bye
现在一切正常!谢谢你再见
#1
15
You're trying to use view
before it has been initialized. You need to wait until after view = (WebView) findViewById(R.id.webView);
has been called in order to initialize onServerReceive
.
您正在尝试在初始化之前使用视图。你需要等到view =(WebView)findViewById(R.id.webView)之后;已被调用以初始化onServerReceive。
EDIT: Looking at this some more what's more likely happening is that you're using your instance of onServerRecieve
before view
is initialized in onCreate()
.
编辑:再看看这个更可能发生的事情是你在onCreate()中初始化视图之前使用你的onServerRecieve实例。
@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
13
Finally found the answer and the problem
终于找到了答案和问题
I had this
我有这个
view.addJavascriptInterface(new MainActivity(), "access_android");
and in web js I accessed android with this "access_android.websocket_emit()"
在web js我用这个“access_android.websocket_emit()”访问了android
and i think because of "new MainActivity()"
it was creating a new class where "view" wasn't defined. so I replaced "new MainActivity()"
with "this"
我认为,由于“新的MainActivity()”,它正在创建一个新的类,其中“视图”未定义。所以我用“this”替换了“new MainActivity()”
view.addJavascriptInterface(this, "access_android");
now all works fine ! thanks good bye
现在一切正常!谢谢你再见