我正在尝试将(通过setter)对象传递给另一个类以便稍后与他做某事,但是当我调用这个对象方法时,我得到空指针异常。
当然,对象的值赋值比调用setFlexInfo函数更早。
Tab1Preview.java
DatabaseTierFlex dbFlex = new DatabaseTierFlex(getContext());
tier = dbFlex.getTier(summoner.getSummonerId());
jsInterface.setTierFlex(tier); // passing object
jsInterface.callJavaScript(webView, "setServer", summoner.getServer());
WebAppInterface.java
private Tier tierFlex; // declaration
private Context mContext;
private NestedWebView webView;
public WebAppInterface(NestedWebView webView, Context c) {
this.mContext = c;
this.webView = webView;
}
public void setTierFlex(Tier tierFlex) { // Setter
this.tierFlex = tierFlex;
}
@JavascriptInterface
public void setFlexInfo() { // method what I'm calling when I click a certain div
webView.post(new Runnable() {
@Override
public void run() {
// I'm getting NullPointerException here
callJavaScript(webView, "setWinsLosses", tierFlex.getWins(), tierFlex.getLosses());
if (tierFlex.getSeriesTarget() != -1) {
callJavaScript(webView, "setPromo", tierFlex.getSeriesTarget(), tierFlex.getSeriesProgress());
}
if (tierFlex.isFreshBlood()) {
callJavaScript(webView, "setEmblemFreshBlood");
}
if (tierFlex.isVeteran()) {
callJavaScript(webView, "setEmblemVeteran");
}
if (tierFlex.isHotStreak()) {
callJavaScript(webView, "setEmblemHotStreak");
}
}
});
}
public void callJavaScript(WebView view, String methodName, Object... params) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("javascript:try{");
stringBuilder.append(methodName);
stringBuilder.append("(");
String separator = "";
for (Object param : params) {
stringBuilder.append(separator);
separator = ",";
if (param instanceof String) {
stringBuilder.append("'");
}
stringBuilder.append(param);
if (param instanceof String) {
stringBuilder.append("'");
}
}
stringBuilder.append(")}catch(error){console.error(error.message);}");
final String call = stringBuilder.toString();
Log.i(TAG, "callJavaScript: call=" + call);
view.loadUrl(call);
}
编辑:堆栈跟踪
05-16 22:56:38.481 27438-27438 / com.example.krulkamyl.accountinfo E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.krulkamyl.accountinfo,PID:27438 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int com.example.krulkamyl.accountinfo.model.Tier.getWins()' 在com.example.krulkamyl.accountinfo.interfaces.WebAppInterface $ 3.run(WebAppInterface.java:164) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:145) 在android.app.ActivityThread.main(ActivityThread.java:6934) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
WebAppInterface.java:164 - callJavaScript(webView,“setWinsLosses”,tierSolo.getWins(),tierSolo.getLosses());
答案 0 :(得分:1)
NPE被抛入可能异步执行的runnable中。确保永远不会使用参数null调用setter,这将导致tierFlex被取消设置。 最终成功可能是一个好主意。这可能需要一些额外的重构。