My code ran fine on the emulator, however, as soon as I tried running my code on a device (in this case a Nexus 5) I started to get A LOT of Resources.NotFoundExceptions. The majority of the exceptions occur in relation to drawable or color files. For example:
android.content.res.Resources$NotFoundException: File res/drawable/blob_ocean.xml from drawable resource ID #0x7f060066
Logcat then states that the error is caused by:
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/blob_ocean.xml from drawable resource ID #0x7f060066
then it states that the previous error is caused by:
Caused by: android.content.res.Resources$NotFoundException: File res/color/radial_pink.xml from color state list resource ID #0x7f04004e
The two examples occur in a fragment on the line with the AnimatedVectorDrawable.
public class BlobFragment extends Fragment {
private AnimatedVectorDrawable blob;
private ImageView blobView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.blob_fragment, container, false);
blobView = (ImageView) v.findViewById(R.id.blob_o);
blob = (AnimatedVectorDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.blob_ocean, null);
blobView.setImageDrawable(blob);
blob.start();
return v;
}
}
The layout for the fragment looks like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OceanThemeActivity">
<ImageView
android:id="#+id/blob_o"
android:layout_width="300dp"
android:layout_height="300dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:srcCompat="#drawable/blob_ocean"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.271"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I have looked online but have not been able to find something that works. Having a hard time identifying the issue as everything is working fine in an emulator.
I solved the issue. It was related to backward compatibility; I was using AnimatedVectorDrawable which is only working for API 24 or higher and my device was lower. Changing the objects to AnimatedVectorDrawableCompat fixed it. Also, some of the drawable recourses were too big, making them smaller worked perfectly fine.
Related
On some Android devices, I am getting "Resources$NotFoundException: Drawable (missing name) with resource ID" error, e.g.:
On LGE Android 8.1 devices it is complaining
Unable to start activity ComponentInfo{SplashActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class ImageView
For the following part. In particular, it is complaining "android:src="#drawable/red_logo""
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="#drawable/red_logo" />
I checked the source code directory, "red_logo.png" already exists in the following folders:
drawable
drawable-hdpi
drawable-ldpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Here is the Java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash); <---- Fails here
}
The dimension of red_logo.png in drawable folder is 4864 × 2692, same size as the red_logo.png in drawable-xxxhdpi folder. Is it too big?
I did a lot of research but it seems I cannot find a valid answer.
Thank you.
Best regards.
It happens due to scale issues, hence, always use a vector asset instead of an image in such scenarios. Worked for me.
I create an app using binding and I get an error that I couldn't solve.
The error is
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newtest/com.example.mylibrary.MainActivity1}: java.lang.NullPointerException: Missing required view with ID: com.example.newtest:id/myRecyclerview
Thanks!
public class MainActivity1 extends AppCompatActivity {
RecyclerView.LayoutManager layoutManager;
ArrayList<POJOimage>pojoImageArrayList;
ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
layoutManager = new LinearLayoutManager(this);
binding.myRecyclerview.setLayoutManager(layoutManager);
init();
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity1"
android:background="#000"
android:padding="10dp">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myRecyclerview"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I strongly suspect some Android Studio bug.
I had the same bug, and everything I've had done was adding some TextView.
As it was easy to find this difference, I've just started fiddling with this TextView in xml. At some point bug disappeared.
And here comes the best. Before starting to deal with the problem I've saved problematic change with git. So at the end I've reapplied this change and ... everything worked. No more bug !
P.S. As this bug reappears, I've found that the easiest solution for getting rid of it is to use "Clean Project" from "Build" menu.
I am doing a java to c# transition, and need help.
in Visual Studio 2019 Pro, Android 9.0 (Pie), I am doing this example:
https://learn.microsoft.com/en-us/xamarin/android/platform/binding-java-library/binding-a-jar
The goal is later to convert my java libs to c# for my big project.
I follow the instructions to the letter, and when I come to the part where I need to create an ImageView, there is the problem. Once I create the ImageView, it has an android:src field, something like this (auto generated):
android:src="#drawable/icon"
The problem here, is when I remove that field, in the VS designer, the ImageView disappears and it does not matter if I have the c# code or not, even when I set the srec to:""
Bellow is literally all the code in MainActivity:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Com.Squareup.Picasso;
namespace App3
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
/***********************************************************************/
/**Picasso Code**/
ImageView imageView = FindViewById<ImageView>(Resource.Id.imageView1);
// Use the Picasso jar library to load and display this image:
Picasso.With(this)
.Load("http://i.imgur.com/DvpvklR.jpg")
.Into(imageView);
/**End Ff Picasso Code**/
/***********************************************************************/
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:src="#drawable/icon" <!-- this line -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView1" />
</LinearLayout>
The app does nothing, and in the emulator, the ImageView is empty.
And there is no error, or anything.
If you need anything more, please let me know.
I hope you can help me, if not, thank you for your time.
Edit 1:
It seems, the problem lies in the C# code.
If I add the android:src field, it still does not show up on the emulator, but when I remove the code (the Picasso code) than the image(drawable/icon) shows. Still no error!!
try this
<ImageView
android:src="#drawable/icon" <!-- this line -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="50px"
android:minHeight="50px"
android:id="#+id/imageView1" />
I get this error:
android.content.res.Resources$NotFoundException · String resource ID #0x2040003
and in the full stack I saw that it depends on:
Resources.java:322android.content.res.Resources.getText
I know probably the error is that I call somewhere a setText not passing a string.
My problem is that I do not know where the problem occurs.
Technically the line of the error is this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_web, container, false); // <-- HERE IS THE ERROR
}
The layout file is:
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.WebFragment" />
I do not know howto find the line code where the error occurs.
It might be related to this issue: https://issuetracker.google.com/issues/141132133 regarding Webview.
It happened to me as well, in my case it's MarkdownView which extends Webview. Apparently upgrading android appcompat library solve the issue, but still, I'm hoping for another solution for current library version.
Because I use the element which caused crash in 2 different place (with same implementation) and it only crash in 1 of them. I've been spending my time searching about this and still dunno why and how to fix it.
The following classes could not be instantiated:
- com.amazon.device.ads.AdLayout (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
java.lang.NoClassDefFoundError: Could not initialize class android.os.Environment
at com.amazon.device.ads.DebugProperties.readDebugProperties(DebugProperties.java:75)
at com.amazon.device.ads.InternalAdRegistration.<init>(InternalAdRegistration.java:52)
at com.amazon.device.ads.InternalAdRegistration.getInstance(InternalAdRegistration.java:64)
at com.amazon.device.ads.AdLayout.initialize(AdLayout.java:185)
at com.amazon.device.ads.AdLayout.initialize(AdLayout.java:176)
at com.amazon.device.ads.AdLayout.<init>(AdLayout.java:120)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0( at sun.reflect.NativeConstructorAccessorImpl.newInstance( at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( at java.lang.reflect.Constructor.newInstance( at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:755)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
My main.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:Amazon="http://schemas.android.com/apk/lib/com.amazon.device.ads"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="top|center_horizontal" >
<com.amazon.device.ads.AdLayout
android:id="#+id/myAdView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
My MainActivity.java is this
import com.amazon.device.ads.AdLayout;
import com.amazon.device.ads.AdRegistration;
import com.amazon.device.ads.AdTargetingOptions;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
int defaultValue = R.drawable.blue;
int themedefault = ThemeChanger.THEME_BLUE;
appliedtheme = preferences.getInt("mytheme", themedefault);
ThemeChanger.onActivityCreateSetTheme(this, appliedtheme);
setContentView(R.layout.main);
AdRegistration.setAppKey("Application_Key");
this.adView = (AdLayout) findViewById(R.id.myAdView);
this.adView.loadAd(new AdTargetingOptions());
button1 = preferences.getInt("DigitButtonStyle", defaultValue);
buttonmadd = preferences.getInt("MemoryButtonStyle", defaultValue);
buttoncos = preferences.getInt("FunctionButtonStyle", defaultValue);
I had the same issue and followed Amazon developer guide. To solve this issue, just copy amazon library and paste it in your libs folder and than run.
It's a known issue in Eclipse ADT plugin, even if you use View.isInEditMode(), in some cases, it crashes in recent API versions (higher than 18). It's fixed in Android Studio.
If you want to see Layout Preview, you can try with different API version lower than 19.
Check the screenshot. It's using API 18. (Red box is Amazon AdLayout)