问题
I recently updated my phone to Android 9.0. The app which I was developing was working fine in earlier Android version. But after the update some views which require internet are not loading. Blank spaces are being displayed in their places.
It has a ViewPager
as banner and 2 RecyclerView
below categories and deals respectively. They all require internet to load data.
I don't know know if any extra permissions or dependencies are required in Android Pie.
The logs show
NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
回答1:
Try these solutions
Solution 1)
Add this line in application tag in manifest
file
android:usesCleartextTraffic="true"
like below
<application
android:name=".ApplicationClass"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Add this tag in application tag in manifest
file
Solution 2)
Add android:networkSecurityConfig="@xml/network_security_config"
in application tag
<application
android:name=".ApplicationClass"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:theme="@style/AppTheme">
where network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Create xml under res directory and then network_security_config.xml
in xml
folder
For more info
https://developer.android.com/about/versions/pie/android-9.0-changes-28
Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)
回答2:
The only possible error i'm thinking is . after Android 9 you need to specify networksecurityconfig
to connect with server url . So you need to make following changes in your code .
Add android:networkSecurityConfig="@xml/network_security_config"
in application tag in your AndroidManifest.xml
like this
<application
android:name=".ApplicationClass"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:theme="@style/AppTheme">
where network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
you need to Create xml
directory under res
directory and then network_security_config.xml
in xml folder
回答3:
To enhance user privacy, Android 9 introduces several behavior changes, such as limiting background apps' access to device sensors, restricting information retrieved from Wi-Fi scans, and new permission rules and permission groups related to phone calls, phone state, and Wi-Fi scans.
These changes affect all apps running on Android 9, regardless of target SDK version.
android:usesCleartextTraffic
Indicates whether the app intends to use cleartext network traffic, such as cleartext HTTP. The default value is "true". When the attribute is set to "false", platform components (for example, HTTP and FTP stacks, DownloadManager, MediaPlayer) will refuse the app's requests to use cleartext traffic. Third-party libraries are strongly encouraged to honor this setting as well. The key reason for avoiding cleartext traffic is the lack of confidentiality, authenticity, and protections against tampering: a network attacker can eavesdrop on transmitted data and also modify it without being detected.
This flag is honored on a best effort basis because it's impossible to prevent all cleartext traffic from Android applications given the level of access provided to them. For example, there's no expectation that the Socket API will honor this flag because it cannot determine whether its traffic is in cleartext. However, most network traffic from applications is handled by higher-level network stacks/components which can honor this flag by either reading it from ApplicationInfo.flags or NetworkSecurityPolicy.isCleartextTrafficPermitted().
来源:https://stackoverflow.com/questions/54720353/views-not-loading-after-updating-device-to-android-pie-9-0