This is the 24th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Solve the Android Studio cannot import Android. Support. The v4. App. Fragments

Question:

Running someone else’s project code when the import android. Support. The v4. App. The fragments according to gray, is no guide to come in, refer to the online method, in turn, click:

Solutions:

File -> Product Structure -> Dependencies ->

Select the project -> click the + sign -> Library Dependency -> Search support-V4 ->

Select com.android.support -> select the appropriate version and click OK

If invalid, the import android. Support. The v4. App. Fragments or gray, to continue to look at:

Because the new version of android uses packages from androidx by default, instead of the previous support package, if automatic package guide is enabled, the package from androidx will be automatically imported when using ViewPager + fragment

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
Copy the code

Workaround: Remove the default androidX package

Switch the view to Project and go to Gradle. Prooerties

android.useAndroidX=true
android.enableJetifier=true
Copy the code

Instead of

android.useAndroidX=false
android.enableJetifier=false
Copy the code

Or just delete those two sentences.

After the change, other packages starting from androidx should also be changed to support.

For example, the package for AppCompatActivity needs to be changed

import android.support.v7.app.AppCompatActivity;
Copy the code

TransformDexArchiveWithExternalLibsDexMergerForDebug solution

Question:

This error was reported when the Android project was running

TransformDexArchiveWithExternalLibsDexMergerForDebug error shown below is com. Google. Gson. JsonSyn error

Solutions:

File | Settings | Build, Execution, Deployment | Instant Run

Uncheck to disable instant running in the project, problem solved!



WindowManager$BadTokenException: Unable to add window … Reasons and solutions

Problem Description:

The program is used to open the phone camera, but when running the software, the Windows Manager$BadTokenException and the software forced exit

Cause of the problem:

In Android version 6.0 or later, the SYSTEM_ALERT_WINDOW cannot be displayed in the top layer due to a problem with the SYSTEM_ALERT_WINDOW permission. This exception will occur during debugging

Solutions:

Simple solution: Switch to android version, set targetSdkVersion to 22 in build.gradle, problem solved

Other methods: Add the following code to the onCreate () method

       // Let the user manually authorize
       if (Build.VERSION.SDK_INT >= 23) {
            if(! Settings.canDrawOverlays(MainActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:"+ getPackageName())); startActivity(intent); }}Copy the code


java.net.UnknownServiceException: CLEARTEXT… The solution

Question:

The following error was reported when the remote interface was invoked for network communication:

W/System.err: java.net.UnknownServiceException:  CLEARTEXT communication to xxx.xxx.xxx not permitted by network security policy

The reason:

In order to protect user data and devices, Google will require encrypted connections to be used by default for the next generation of Android applications. This means that Android P will prohibit apps from using all unencrypted connections. Therefore, Android devices running Android P will not be able to receive or send traffic with explicit codes in the future, and will need to use the next generation Transport Layer Security protocol, while Android Nougat and Oreo will not be affected. On Android P devices, if an application uses HTTP network requests with unencrypted plaintext traffic, the application cannot make network requests, and HTTPS is not affected. Similarly, if an application has nested WebViews, the WebView can only use HTTPS requests.

Solutions:

(1) The APP changes to HTTPS request

(2) targetSdkVersion drops below 27

(3) Add:

Android: usesCleartextTraffic ="true"Copy the code



java.net.UnknownServiceException: CLEARTEXT… The solution

Exception:

Error running Android Debugger (8600): Unable to open debugger port (localhost:8600): java.net.ConnectException “Connection refused: connect”

Solutions:

adb kill-server
adb start-server
Copy the code