当我使用事件onPause
和/或onResume
时,这不起作用 - crash
- 。
public class xx extends Fragment {
private WebView wv;
private WebView wv2;
public static final String TAG = "xx";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.livewithchat, container, false);
WebView wv = (WebView) rootView.findViewById(R.id.webView);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.loadUrl("XX");
/* Chat */
WebView wv2 = (WebView) rootView.findViewById(R.id.webView2);
wv2.getSettings().setJavaScriptEnabled(true);
wv2.getSettings().setSaveFormData(true);
wv2.getSettings().setBuiltInZoomControls(false);
wv2.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
}
});
wv2.setWebViewClient(
new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false;
}
});
wv2.loadUrl("XX");
wv2.addJavascriptInterface(new WebSocketFactory(wv2), "WebSocketFactory");
return rootView;
}
@Override
public void onResume()
{
super.onResume();
wv.onResume();
wv2.onResume();
}
@Override
public void onPause()
{
super.onPause();
wv.onPause();
wv2.onPause();
}
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
}
Logcat:
07-05 20:37:46.313 11757-11757/com.clemb.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.clemb.test, PID: 11757
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.onResume()' on a null object reference
at com.clemb.test.livewithchat.onResume(livewithchat.java:68)
at android.app.Fragment.performResume(Fragment.java:2192)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:956)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1580)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:451)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
我的片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="228dp"
android:id="@+id/webView" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView2" />
</LinearLayout>
我尝试更改"private wv"
,但这并没有解决问题。
我在其他活动中使用其他webview
(我想创建一个WEB应用程序),这也是同样的问题。
我在StackOverflow中搜索但我找不到解决方案。
答案 0 :(得分:1)
因为public class solutionsTest {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
int allsolutions[][][][][] = new int[16][16][16][16][16];
for (int i = 0; i <= 15; i++) {
for (int j = 0; j <= 15; j++) {
for (int k = 0; k <= 15; k++) {
for (int l = 0; l <= 15; l++) {
allsolutions[i][j][k][l][j] = i;
System.out.println("Set index " + i + " " + j + " " + k + " " + l + " " + j + " equal to " + i);
}
}
}
}
System.out.println(allsolutions[4][2][1][4][5]);
System.out.println(allsolutions[5][2][1][4][5]);
System.out.println(allsolutions[6][2][1][4][5]);
System.out.println(allsolutions[7][2][1][4][5]);
System.out.println(allsolutions[8][2][1][4][5]);
}
}
和wv
为空,您将它们初始化为局部变量而不是使用实例变量,更改:
wv2
到
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
WebView wv = (WebView) rootView.findViewById(R.id.webView);
...
/* Chat */
WebView wv2 = (WebView) rootView.findViewById(R.id.webView2);
这样你就可以使用实例变量来阻止你获得NullPointerExceptions。
<强>更新强>
查看这篇文章,该文章对本地和实例变量https://stackoverflow.com/a/2088370/1333516
之间的区别有相当好的解释