Using toast with pictures on Android 11, toast.getView () is null. After checking, the API has banned some methods in Android 11.
The solution is as follows:
public static void showCenter(Context context, String text) {
Toast toast = Toast.makeText (context, text, Toast.LENGTH_ Short);
// in the upper left corner. If you want to move to the right, set the second parameter to & gt; 0; move down and increase the third parameter; the last two parameters only have pixels
if( Build.VERSION.SDK_ INT < Build.VERSION_ CODES.R) {
toast.setGravity ( Gravity.CENTER , 0, 0);
LinearLayout layout = (LinearLayout) toast.getView ();
ImageView image = new ImageView(context);
image.setImageResource (R. mipmap.icon_ success);
layout.addView (image, 0);
}
toast.show ();
}
Read More:
- Solution of toast not displaying in Android
- Error: Error parsing D:\new_android\Android SDK\system-images\android-25\android-wear\armeabi-v7a\de
- Android Studio | Failed to find target with hash string ‘android-26’ in: D:\Android\sdk
- How to use Android android:supportsRtl attribute
- Android Studio Error: Invoke-customs are only supported starting with Android O (–min-api 26)
- Android project error: could not resolve com.android.support .c onstraint:constraint-layout :2.0.2.
- Android Error | Failed to find target with hash string ”android-23′ in…
- Android android.intent.category Purpose and use of. Launcher
- When Android Studio runs the app, it prompts Error: Please select Android SDK
- Android Studio sync build.gradle appears: Failed to resolve: com.android.support:appcompat problem
- Android studio compilation error: style attribute ‘@ android:attr/windowEnterAnimation ‘not found the ultimate solution
- Android 9.0 APP Install Android 4.4 Error: classnotfoundexception
- Error in Cordova project execution command after Android studio upgrade: could not find gradle wrapper within Android SDK
- Android studio AAPT: error: attribute android:requestLegacyExternalStorage not found.
- android studio:Error inflating class android.support.v4.widget.SwipeRefreshLayout
- How to Fix com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed
- Error inflating class in WebView in Android 5. X android.webkit.WebView Solutions
- Android Studio Series: after Android studio is opened, the project window will not be displayed.
- [error record] as compilation error (Android support plugin version is too high | upgrade Android studio to the latest version)
- Android Error:AAPT: error: resource android:attr/colorError not found