Android应用崩溃与误差
AndroidRuntime: Process: com.example.myapplication, PID: 30895
AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to read from null array
AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(Unknown Source:621)
AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(Unknown Source:47)
AndroidRuntime: at android.app.servertransaction.LaunchActivityItem.execute(Unknown Source:63)
AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(Unknown Source:77)
AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(Unknown Source:73)
AndroidRuntime: at android.app.ActivityThread$H.handleMessage(Unknown Source:43)
AndroidRuntime: at android.os.Handler.dispatchMessage(Unknown Source:19)
AndroidRuntime: at android.os.Looper.loop(Unknown Source:242)
AndroidRuntime: at android.app.ActivityThread.main(Unknown Source:101)
AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
AndroidRuntime: at com.android.internal.os.ZygoteInit.main(Unknown Source:275)
AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from null array
AndroidRuntime: at com.example.myapplication.MainActivity.prepareData(MainActivity.java:70)
AndroidRuntime: at com.example.myapplication.MainActivity.onCreate(MainActivity.java:35)
AndroidRuntime: at android.app.Activity.performCreate(Unknown Source:19)
AndroidRuntime: at android.app.Activity.performCreate(Unknown Source:1)
AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Unknown Source:3)
AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(Unknown Source:376)
我检查了MainActivity.java第70行,它是一个执行https网络请求的函数我确实添加了
android:networkSecurityConfig="@xml/network_security_config
和
放在清单中<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这是导致nullpointer异常的函数
public static String GET(String url) {
StringBuilder result = new StringBuilder();
HttpURLConnection.setFollowRedirects(true);
try {
URL request_url = new URL(url);
HttpURLConnection http_conn = (HttpURLConnection) request_url.openConnection();
http_conn.setConnectTimeout(100000);
http_conn.setReadTimeout(100000);
http_conn.setInstanceFollowRedirects(true);
http_conn.setRequestMethod("GET");
http_conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(http_conn.getInputStream()))) {
for (String line; (line = reader.readLine()) != null; ) {
result.append(line);
}
}
return result.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
这是我的xml/network_security_config
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</base-config>
</network-security-config>
这只发生在某些设备上,当我在模拟器中测试它时,它没有崩溃
崩溃的原因在错误日志中,
AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from null array
你正在尝试访问一个尚未填充的数组,并且是null,这会抛出一个异常并导致应用程序崩溃,最好通过try catch来处理该异常以防止这种情况。
编辑2:
根据评论中的讨论,似乎你没有从网站获得数据,因为无法连接到网站的原因是证书问题,完全通过这一点,我强烈建议不要通过禁用SSL检查,
代码:
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
// Now you can access an https URL without having the certificate in the truststore
try {
URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {
}
在最底部的尝试代码中,你可以使用你创建的函数来调用网站并测试它。
由@Yishai Answer提供