Android的;得到一个"应用程序意外停止"在运行期间

时间:2017-04-06 13:55:08

标签: java android android-intent layout

好的,我有最简单的android工作室应用程序......它杀了我!我不是Android或Java开发人员。

我已经在网上搜索了,我发现了关于在清单中声明活动的旧帖子,我做到了!

我有3项活动;

  1. Splashscreen,运行得很好。
  2. WEMProject,运行得很好。
  3. NoNetworkConnections,"应用程序意外停止" 在运行期间。
  4. 在我的WEMProject中,我只想加载一个Webview,如果我有一个网络连接,如果没有,显示一个很好的"没有网络存在"屏幕。

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if (haveNetworkConnection()) {
            setContentView(R.layout.activity_wemproject);
            myWebView = (WebView) findViewById(R.id.WebView);
    
            myWebView.getSettings().setLoadsImagesAutomatically(true);
            myWebView.getSettings().setJavaScriptEnabled(false);
            myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            myWebView.loadUrl("https://my.wem.io");
        } else {
            Intent intent = new Intent(WEMProject.this, NoNetworkConnection.class);
            startActivity(intent);
        }
    }
    

    这很好用,网络检查工作正常,但如果没有网络:

            Intent intent = new Intent(this, NoNetworkConnection.class);
            startActivity(intent);
    

    部分导致错误..为什么?

    活动是:

    public class NoNetworkConnection extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_no_network_connection);
    } }
    

    我已在AndroidManifest.XML

    中声明了该活动
        <activity
            android:name=".NoNetworkConnection"
            android:parentActivityName=".WEMProject"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_no_network_connection"
            android:theme="@style/FullscreenTheme">
        </activity>
    

    android日志的调试部分:

    04-06 14:12:39.668 15630-15630/io.wem.my.mywem E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: io.wem.my.mywem, PID: 15630
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{io.wem.my.mywem/io.wem.my.mywem.WEMProject}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.NetworkInfo.getTypeName()' on a null object reference
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.NetworkInfo.getTypeName()' on a null object reference
                                                                     at io.wem.my.mywem.WEMProject.haveNetworkConnection(WEMProject.java:33)
                                                                     at io.wem.my.mywem.WEMProject.onCreate(WEMProject.java:61)
                                                                     at android.app.Activity.performCreate(Activity.java:6679)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
    

    按照要求使用haveNetworkConnection()方法:

    private boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;
    
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
    
        if (netInfo.getTypeName().equalsIgnoreCase("WIFI"))
            if (netInfo.isConnected())
                haveConnectedWifi = true;
        if (netInfo.getTypeName().equalsIgnoreCase("MOBILE"))
            if (netInfo.isConnected())
                haveConnectedMobile = true;
    
        return haveConnectedWifi || haveConnectedMobile;
    }
    

    我错过了什么?

1 个答案:

答案 0 :(得分:0)

Quoting Android's ConnectivityManager documentation on getActiveNetworkInfo():

Returns | a NetworkInfo object for the current default network or null if no default network is currently active

So, it may well return null and your code should handle this case.

For example:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();

if (netInfo != null) {
    if (netInfo.getTypeName().equalsIgnoreCase("WIFI"))
        if (netInfo.isConnected())
            haveConnectedWifi = true;
        if (netInfo.getTypeName().equalsIgnoreCase("MOBILE"))
            if (netInfo.isConnected())
                haveConnectedMobile = true;
}