Reputation: 11437
I am converting a Eclipse project to work in Android Studio and have got all issues fixed except some layout. XML is showing the following issue in the drag and drop view
Rendering issues: The following classes cannot be instantiated X.X.X.myclass.
I checked my class and it seems ok, I changed the latest API I have from the little dropdown; 22 (I guess this is the compile API). I haven't changed my Gradle setup, could it be something in there?
I haven't posted any code as I'm not sure what would be helpful - any ideas?
Upvotes: 0
Views: 257
Reputation: 39846
First of all: that's just a layout preview. You can edit the XML and carry on with your project and it won't affect anything on it.
The layout rendering thing literally runs your Java classes to create the preview with some mocked implementation or an actual android device, similar to testing mocked implementations.
So this message is just telling you that this mocked system failed to render the preview.
But if you really want to see the preview, you must check where in your class are you relying on variables, or objects that are inherent from your app that the mock system will not have access to.
An example, if your custom view does some special stuff during onLayout:
@Override onLayout(...){
int value = MyLayoutDetailsCalculation.getExtraPadding(getContext());
}
that is a code that is calling to a static method on a separate class that is using the context (probably getting values from system resources or from the display manager) and this will not execute well in a mocked environment and the preview wouldn't be able to render it.
luckly it's an easy fix:
@Override onLayout(...){
int value;
if(isInEditMode()){ // that returns true if this code is being executed by AndroidStudio mocked system.
value = 0; // any value that makes OK for your custom class to properly show a preview
} else {
value = MyLayoutDetailsCalculation.getExtraPadding(getContext());
}
}
Upvotes: 0