在设备上启动没有互联网连接的Android应用程序会产生FileNotFoundException

时间:2013-11-07 13:44:29

标签: android mysql internet-connection

在这些特定情况下,我的应用无法启动:

  • 没有互联网连接
  • 在设备上发布

并给出一个FileNotFoundException,因为我没有任何权限(请参阅下面的堆栈跟踪)。在其他情况下这是有效的。我从mysql数据库中检索我的数据。 在主要活动的onCreate方法中,我检查互联网连接。

我在清单中有这些权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

我正在检查主要活动中的互联网连接:

private boolean isNetworkAvailable() {
         ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
         if(activeNetworkInfo.isAvailable()){
             return true; 
         }
         else {
             return false;
         }

错误讯息:

11-07 14:15:16.176: E/AndroidRuntime(27318): Uncaught handler: thread main exiting due to uncaught exception
11-07 14:15:16.176: E/Monkey(27318): exception :java.io.FileNotFoundException: /dev/kmsg: open failed: EACCES (Permission denied)
11-07 14:15:16.200: E/AndroidRuntime(27318): exception :java.io.FileNotFoundException: /dev/kmsg: open failed: EACCES (Permission denied)
11-07 14:15:16.200: E/AndroidRuntime(27318): exception :java.io.FileNotFoundException: /dev/kmsg: open failed: EACCES (Permission denied)
11-07 14:15:16.200: E/AndroidRuntime(27318): FATAL EXCEPTION: main
11-07 14:15:16.200: E/AndroidRuntime(27318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phi.oudemarkt/com.phi.oudemarkt.MainActivity}: java.lang.NullPointerException

2 个答案:

答案 0 :(得分:0)

请改用:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

答案 1 :(得分:0)

尝试添加此类权限

uses-permission android:name =“android.permission.READ_EXTERNAL_STORAGE