Tried to make it short but have to explain in detail. I have one small project where I have one activity and multiple fragments. While launching, first fragment added and works fine. After that when I navigated to another fragment by using replace, another fragment opens but it does not display any UI at all. When i debugged it, then while inflating it says Resource not found
without any extra details. While in log it's Invalid ID XXXXXXXXX
where XXXXXXXXX
is the id number but I am not able to find which id it is because as there is no R file in android studio.
Also tried to use analyze apk
but did not find any id with that number there as well with no luck.
Also tried clean build, with no luck.
Please do help here.
CodePudding user response:
I've seen this kind of errors many times here in Stack Overflow. It may caused by passing an invalid/wrong resource id e.g. R.id
instead of R.layout
but most the time it because of non-existing ids like following example:
int value = 999;
Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
Even though the code compiles just fine, but as you already know it will throw a RuntimeException.
Anyway, you can check your ids with the following code:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StringBuilder sb = new StringBuilder();
Class clazz = R.class;
Class[] classes = clazz.getDeclaredClasses();
for(int m=0; m < classes.length; m) {
sb.append("Class name: " classes[m].getSimpleName() "\n");
Field[] fields = classes[m].getDeclaredFields();
sb.append("Number of fields: " fields.length "\n");
try {
for(int n=0; n < fields.length; n)
sb.append(String.format("Field[%d]: %s=0xx\n", n 1, fields[n].getName(), fields[n].getInt(fields[n])));
} catch(Exception e) {}
}
Log.d("TAG", sb.toString());
//Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
CodePudding user response:
looks for me like your app doesn't have a layout file. It only shows something if you have a Layout file that tells Android Studio how the fragment is supposed to look like. Make in the Layout folder a document for it and connect it to the fragment.
you can see I have for every single of the classes/Fragnments a Layout file that tells Android Studio how to show every part of the class, shape and color of buttons, TextViews and more.
You then connect it to the class so the class knows where it's layout is
I hope that is going to help you