It only started happening recently after I moved some of the elements in my layout around. I can share the XML code too if you think that would help solve the problem.
Here's the Activity that caused the exception:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="#+id/relativeLayout1"
p1:background="#drawable/pickcitybackground">
<ImageView
p1:src="#drawable/pick"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="#+id/pick"
p1:layout_marginLeft="50dp"
p1:layout_marginTop="90dp" />
<ImageButton
p1:src="#drawable/austinbutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="#+id/austinButton"
p1:background="#02ffffff"
p1:scaleType="centerCrop"
p1:layout_marginTop="90dp"
p1:layout_marginLeft="210dp" />
<ImageButton
p1:src="#drawable/nashvillebutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="#+id/nashvilleButton"
p1:scaleType="centerCrop"
p1:background="#02ffffff"
p1:layout_marginLeft="50dp"
p1:layout_marginTop="265dp" />
<ImageButton
p1:src="#drawable/newyorkbutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="#+id/newyorkButton"
p1:scaleType="centerCrop"
p1:background="#02ffffff"
p1:layout_marginTop="265dp"
p1:layout_marginLeft="210dp" />
<ImageButton
p1:src="#drawable/sanfranbutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="#+id/sanfranButton"
p1:scaleType="centerCrop"
p1:background="#02ffffff"
p1:layout_marginTop="435dp"
p1:layout_marginLeft="50dp" />
<ImageButton
p1:src="#drawable/seattlebutton"
p1:layout_width="60dp"
p1:layout_height="60dp"
p1:id="#+id/seattleButton"
p1:layout_marginLeft="210dp"
p1:background="#02ffffff"
p1:scaleType="centerCrop"
p1:layout_marginTop="435dp" />
The stack trace:
Android.Views.InflateException: Binary XML file line #1: Error inflating class android.widget.ImageButton ---> Java.Lang.Reflect.InvocationTargetException: ---> Java.Lang.OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free bytes and 74MB until OOM
at java.lang.OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free bytes and 74MB until OOM
at at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at at android.content.res.Resources.loadDrawable(Resources.java:2381)
at at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at at android.widget.ImageView.<init>(ImageView.java:146)
at at android.widget.ImageButton.<init>(ImageButton.java:86)
at at android.widget.ImageButton.<init>(ImageButton.java:82)
at at android.widget.ImageButton.<init>(ImageButton.java:78)
at at java.lang.reflect.Constructor.newInstance(Native Method)
at at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at at android.app.Activity.setContentView(Activity.java:2145)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.n_onCreate(Native Method)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.onCreate(pickCityPage.java:29)
at at android.app.Activity.performCreate(Activity.java:5990)
at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2415)
at at android.app.ActivityThread.access$900(ActivityThread.java:152)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at at android.os.Handler.dispatchMessage(Handler.java:102)
at at android.os.Looper.loop(Looper.java:135)
at at android.app.ActivityThread.main(ActivityThread.java:5301)
at at java.lang.reflect.Method.invoke(Native Method)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:723)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
--- End of inner exception stack trace ---
at java.lang.reflect.InvocationTargetException
at at java.lang.reflect.Constructor.newInstance(Native Method)
at at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at at android.app.Activity.setContentView(Activity.java:2145)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.n_onCreate(Native Method)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.onCreate(pickCityPage.java:29)
at at android.app.Activity.performCreate(Activity.java:5990)
at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2415)
at at android.app.ActivityThread.access$900(ActivityThread.java:152)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at at android.os.Handler.dispatchMessage(Handler.java:102)
at at android.os.Looper.loop(Looper.java:135)
at at android.app.ActivityThread.main(ActivityThread.java:5301)
at at java.lang.reflect.Method.invoke(Native Method)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:723)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at Caused by: java.lang.OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free bytes and 74MB until OOM
at at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at at android.content.res.Resources.loadDrawable(Resources.java:2381)
at at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at at android.widget.ImageView.<init>(ImageView.java:146)
at at android.widget.ImageButton.<init>(ImageButton.java:86)
at at android.widget.ImageButton.<init>(ImageButton.java:82)
at at android.widget.ImageButton.<init>(ImageButton.java:78)
at ... 25 more
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3819/96c7ba6c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x000a7] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12083
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00068] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:31
at Android.App.Activity.SetContentView (System.Int32 layoutResID) [0x00017] in /Users/builder/data/lanes/3819/96c7ba6c/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.App.Activity.cs:5253
at Quickies.Android.pickCityPage.OnCreate (Android.OS.Bundle bundle) [0x0000e] in /Users/michaelsloan/Desktop/Quickies/Android/Quickies/pickCityPage.cs:28
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in /Users/builder/data/lanes/3819/96c7ba6c/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.App.Activity.cs:2550
at at (wrapper dynamic-method) System.Object:e8fa207e-6c2f-4c69-899e-9bfde46df162 (intptr,intptr,intptr)
at android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.ImageButton
at at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at at android.app.Activity.setContentView(Activity.java:2145)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.n_onCreate(Native Method)
at at md54421ba596fa8cc5324701d9bb904102d.pickCityPage.onCreate(pickCityPage.java:29)
at at android.app.Activity.performCreate(Activity.java:5990)
at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2415)
at at android.app.ActivityThread.access$900(ActivityThread.java:152)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at at android.os.Handler.dispatchMessage(Handler.java:102)
at at android.os.Looper.loop(Looper.java:135)
at at android.app.ActivityThread.main(ActivityThread.java:5301)
at at java.lang.reflect.Method.invoke(Native Method)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:723)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at Caused by: java.lang.reflect.InvocationTargetException
at at java.lang.reflect.Constructor.newInstance(Native Method)
at at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at ... 23 more
at Caused by: java.lang.OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free bytes and 74MB until OOM
at at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at at android.content.res.Resources.loadDrawable(Resources.java:2381)
at at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at at android.widget.ImageView.<init>(ImageView.java:146)
at at android.widget.ImageButton.<init>(ImageButton.java:86)
at at android.widget.ImageButton.<init>(ImageButton.java:82)
at at android.widget.ImageButton.<init>(ImageButton.java:78)
at ... 25 more
You have something in your layout that requires a lot of memory. Probably bitmaps, I don't know without XML layout.
OutOfMemoryError: Failed to allocate a 182250012 byte allocation with 4194304 free
Your images are too large. You should consider using an image loader library (I recommend Glide) to load the images, rather than just letting Android do it (because Android is pretty bad at it.)
EDIT: My GlideImageView:
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
/**
* Created by Zhuinden on 2016.05.07..
*/
public class GlideImageView
extends ImageView {
public GlideImageView(Context context) {
super(context);
init(context, null, 0);
}
public GlideImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public GlideImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
#TargetApi(21)
public GlideImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr);
}
int drawableResource = 0;
private void init(Context context, AttributeSet attributeSet, int defStyle) {
TypedArray a = null;
if(defStyle != 0) {
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.GlideImageView, defStyle, 0);
} else {
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.GlideImageView);
}
drawableResource = a.getResourceId(0, 0);
a.recycle();
ViewUtils.waitForMeasure(this, new ViewUtils.OnMeasuredCallback() {
#Override
public void onMeasured(View view, int width, int height) {
if(!isInEditMode()) {
if(drawableResource != 0) {
Glide.with(getContext()).load(drawableResource).dontAnimate().into(GlideImageView.this);
}
} else {
setImageResource(drawableResource);
}
}
});
}
}
And
public class ViewUtils {
public interface OnMeasuredCallback {
void onMeasured(View view, int width, int height);
}
public static void waitForMeasure(final View view, final OnMeasuredCallback callback) {
int width = view.getWidth();
int height = view.getHeight();
if(width > 0 && height > 0) {
callback.onMeasured(view, width, height);
return;
}
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
final ViewTreeObserver observer = view.getViewTreeObserver();
if(observer.isAlive()) {
observer.removeOnPreDrawListener(this);
}
callback.onMeasured(view, view.getWidth(), view.getHeight());
return true;
}
});
}
private ViewUtils() {
}
}
And
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GlideImageView">
<attr name="image_resource" format="integer"/>
</declare-styleable>
</resources>
Related
i am not using any layout so the all view is created by drawView class then how can i use scrollView in this scenario. so that i can able to scroll and view the whole line.
MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawView drawView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
//this class will draw lines on screen.
public class DrawView extends View {
Paint paint = new Paint();
Bitmap bmp;
public DrawView(Context context) {
super(context);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3f);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.station);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(350,300,350,2500, paint);
for (int i = 315; i< 995;i=i+20)
canvas.drawLine(350,i,370,i,paint);
canvas.drawLine(370,300,370,2500, paint);
canvas.drawBitmap(bmp,350,300,paint);
canvas.drawBitmap(bmp,300,500,paint);
}
}
//I also tried like this
and when using this xml layout in that condition i have used
setContentView(R.layout.activity_main)
activity_main this is my xml layout file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<com.example.shailendra.myapplication.DrawView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
Then i got following errors:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shailendra.myapplication/com.example.shailendra.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.shailendra.myapplication.DrawView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5233)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.shailendra.myapplication.DrawView
at android.view.LayoutInflater.createView(LayoutInflater.java:616)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.shailendra.myapplication.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:6001)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5233)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getConstructor(Class.java:495)
at android.view.LayoutInflater.createView(LayoutInflater.java:580)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.shailendra.myapplication.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:6001)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5233)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) **
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="true">
Put draw View here
</ScrollView>
You can add your drawView with yourDrawer.addView(yourDrawView)
My app have a problem with some devices (namely Asus Zenfone 5 (Intel based processor)). It's keep forced close when opening it. This is the log from the devices:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coolappz.FitPartners/com.coolappz.FitPartners.ui.MainActivity}: android.view.InflateException: Binary XML file line #42: Error inflating class com.coolappz.FitPartners.ui.custom.CustomFontEditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2380)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class com.coolappz.FitPartners.ui.custom.CustomFontEditText
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.coolappz.FitPartners.ui.MainActivity.onCreate(MainActivity.java:165)
at android.app.Activity.performCreate(Activity.java:6018)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2273)
... 10 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
... 22 more
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 96
at android.content.res.TypedArray.getColor(TypedArray.java:401)
at android.widget.TextView.<init>(TextView.java:717)
at android.widget.EditText.<init>(EditText.java:65)
at android.widget.EditText.<init>(EditText.java:61)
at android.widget.EditText.<init>(EditText.java:57)
at com.coolappz.FitPartners.ui.custom.CustomFontEditText.<init>(CustomFontEditText.java:18)
... 25 more
The problem is, it's only error on that devices. I've tried the app in emulators (ARM based and x86 based) and other devices, and it's work fine.
Did anyone have same problem? I use android studio with gradle 2.2.0.
This is the customFontEditText class, and I forgot to mention, when I change the CustomFontEditText to normal EditText, it's still forced close
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* Created by harto on 4/22/2016.
*/
public class CustomFontEditText extends EditText {
public CustomFontEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomFontEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomFontEditText(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/SourceSansPro-Regular.otf");
setTypeface(tf);
}
}
}
I use gradle v2.1.0 when I first build this and it's forced close when I first build it
Downgrading might worth a try:
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
}
I have a custom ImageView Class as below
public class MyImageView extends ImageView
{
public MyImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
canvas.drawLine(0, 0, 20, 20, p);
super.onDraw(canvas);
}
}
and inside my Activity class the Oncreate methode is defined as
MyImageView imageView;
// It works perfect when i use ImageView instead of MyImageView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(MyImageView)findViewById(R.id.image);
Bitmap dbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dinkan);
Bitmap bitmap = dbitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
imageView.setImageBitmap(bitmap);
}
It ends with an RuntimeException caused by InflateException. The error log is shown below
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hari.myapplication/com.example.hari.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class com.example.hari.myapplication.MyImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class com.example.hari.myapplication.MyImageView
at android.view.LayoutInflater.createView(LayoutInflater.java:603)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.hari.myapplication.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getConstructor(Class.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:568)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.hari.myapplication.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method
But it works perfect when i use imageview instead of a custom ImageView(MyImageView). So what is wrong with my code? Any help is appreciated.
When inflating a custom View from XML, you must have all the correct constructors.
try adding these constructors as well as the one you have
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
Im trying to add a custom View to Round Android Wear and am getting the following error from logcat:
12-31 19:27:27.211 2212-2212/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.crashoveride007.roundwatchcanvas, PID: 2212
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crashoveride007.roundwatchcanvas/com.example.crashoveride007.roundwatchcanvas.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.crashoveride007.roundwatchcanvas.MyView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.crashoveride007.roundwatchcanvas.MyView
at android.view.LayoutInflater.createView(LayoutInflater.java:616)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.example.crashoveride007.roundwatchcanvas.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getConstructor(Class.java:495)
at android.view.LayoutInflater.createView(LayoutInflater.java:580)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.example.crashoveride007.roundwatchcanvas.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Here is the MyView Custom View Class:
package com.example.crashoveride007.roundwatchcanvas;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
/**
* Created by Crash.Overide.007 on 2/24/16.
*/
public class MyView extends View{
Paint myPaint;
Bitmap image;
public MyView(Context context) {
super(context);
image = BitmapFactory.decodeResource(getResources(),R.drawable.ok);
myPaint = new Paint();
myPaint.setColor(Color.WHITE);
myPaint.setStrokeWidth(10);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(image,137,0,null);
}
}
The MainActivity Class:
package com.example.crashoveride007.roundwatchcanvas;
import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.round_activity_main);
final RelativeLayout main = (RelativeLayout) findViewById(R.id.roundWatch);
}
}
And The XML for round_activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/roundWatch"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.crashoveride007.roundwatchcanvas.MainActivity"
tools:deviceIds="wear_round">
<com.example.crashoveride007.roundwatchcanvas.MyView
android:id="#+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Can anyone help me in removing this error and move over how to work with canvas in a wear activity?
Thank you.
You need to provide additional constructors for your view to inflate properly. At a minimum, you'll need one that takes both a Context and an AttributeSet as parameters, like this:
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
...
}
According to the View documentation, this "second form should parse and apply any attributes defined in the layout file."
You generally ought to override the other two standard View constructors as well:
http://developer.android.com/reference/android/view/View.html#pubctors
I have implemented a custom TextView, then I need to change its text size for future usages. At that step I decided to make its textSize custom with using style.xml and attr.xml
However I am getting a runtime error while my custom layout is inflating. The error related with the parts that Im changed for making it custom.
Here is the code pieces that I was implemented.
attr.xml
<declare-styleable name="FieldLayout">
<attr name="right_detail_text_size" format="dimension"/>
</declare-styleable>
styles.xml
<style name="TextViewRightDetail">
<item name="android:textSize">?attr/right_detail_text_size</item>
</style>
layout_field_detailed.xml
<com.my.view.TextViewFont
android:id="#+id/layout_fielddetailed_textview_rightdetail"
style="#style/TextViewRightDetail" />
layout_implementation.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iattr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.my.view.FieldLayoutDetailed
android:id="#+id/fieldlayoutdetailed_example"
android:layout_width="match_parent"
android:layout_height="wrap_content"
iattr:right_detail_text_size="16sp"/>
</LinearLayout>
FieldLayoutDetailed.java
public FieldLayoutDetailed(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.FieldLayout, 0, 0);
LayoutInflater.from(context).inflate(R.layout.layout_field_deailed, this, true);
Error that Im getting at Runtime
...
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
at android.widget.TextView.<init>(TextView.java:1003)
at android.widget.TextView.<init>(TextView.java:632)
at android.widget.TextView.<init>(TextView.java:628)
at com.my.view.TextViewFont.<init>(TextViewFont.java:29)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.my.view.FieldLayoutDetailed.<init>(FieldLayoutDetailed.java:50)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.my.ActivityWithMenu.setContentView(ActivityWithMenu.java:77)
at com.my.FastCreditApplyMain.onCreate(FastCreditApplyMain.java:37)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Now I am solved the problem by dynamically change text size, but I am curious about the reason of the problem.
Thank you.
This is your problem:
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
at com.my.view.TextViewFont.<init>(TextViewFont.java:29)
Possible solution: place this into your contructor of TextViewFont.java
TypedArray a = context.obtainStyledAttributes(
attrs,
R.styleable.FieldLayout,
0, 0);
try {
final int fontSize = a.getDimensionPixelSize(R.styleable.FieldLayout_right_detail_text_size, 0);
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
}
finally {
a.recycle();
}
or
TypedArray a = context.obtainStyledAttributes(
attrs,
R.styleable.FieldLayout,
0, 0);
try {
final int fontSize = a.getDimension(R.styleable.FieldLayout_right_detail_text_size, 0);
this.setTextSize(fontSize);
}
finally {
a.recycle();
}