私のコードを終了するのに問題があります。 1つのことを除いて、すべてが機能します。私は呼び出すことができないwebViewClientを参照するnullエラーが発生し続ける。私はWiFiとモバイルネットワークをオフにする場合を除いて、アプリは正常に動作します。ダイアログが表示されるはずですが、アプリは停止します。私はそれが働いていたが、apparantly私は悪いのために何かを変更しました。これは私のエラーです:nullオブジェクト参照でアクティビティ(...)を開始できません
07-12 12:59:33.770 21023-21023/companyname.appname
E/AndroidRuntime: FATAL EXCEPTION: main
Process: companyname.appname, PID: 21023
java.lang.RuntimeException: Unable to start activity ComponentInfo{companyname.appname/companyname.appname.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference
at companyname.appname.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
そして、これは私のコードの一部です:
public class MainActivity extends AppCompatActivity {
WebView browser;
FloatingActionButton fab_plus,fab_settings,fab_share, fab_report;
Animation FabOpen,FabClose,FabRClockwise,FabRantiClockwise;
boolean isOpen = false;
boolean doubleBackToExitPressedOnce = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!isConnected(MainActivity.this)) buildDialog(MainActivity.this).show();
else {
setContentView(R.layout.activity_main);
}
browser=(WebView) findViewById(R.id.webView);
browser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return (true);
}
});
String url = "here's my url";
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
CookieManager.getInstance().setAcceptCookie(true);
view.loadUrl(url);
//Over here there's a lot of code which I think is irrelevant for this problem. And then at the bottom there's my dialog and internet check:
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
else return false;
} else
return false;
}
public AlertDialog.Builder buildDialog(Context c) {
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle("No connection");
builder.setMessage("Text");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
return builder;
}
}
そして、こっちactivity_main XMLファイル内のWebViewの一部:
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
</WebView>
Iそれは私が見ることができない単純なものだと思う。皆さんは何か考えていますか?事前に多くの感謝!
可能な重複[NullPointerExceptionとは何ですか?どうすれば修正できますか?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – litelite