Unable to Inflate EditText in some devices - android

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'
}

Related

Custom View not inflating when using WindowManager in service

I am trying to add a custom KnockView using WindowManager in service. Here is KnockView class.
public class KnowView extends LinearLayout {
public KnowView(Context context) {
super(context);
initView();
}
public KnowView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.KnowView);
try {
isFromChangeKnock = typedArray.getBoolean(R.styleable.KnowView_changeKnock, false);
} catch (Exception e) {
e.printStackTrace();
} finally {
typedArray.recycle();
}
initView();
}
public KnowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
mainLayout = inflate(getContext(), R.layout.knock_code, this);
}
}
So basically in KnowView class we are inflating another layout (knock_code) and adding that into KnowView. And after that, I am using this custom Knockview in another layout called. main_view.xml
<RelativeLayout 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:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:orientation="vertical"
tools:context=".MainActivity">
<production.kado.lock.views.KnowView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="20dp"
app:changeKnock="true" />
</RelativeLayout>
and inflating main_view.xmlin service with WindowManager like this.
WindowManager windowManager = ((WindowManager)
this.getSystemService(WINDOW_SERVICE));
View view1 = LayoutInflater.from(this).inflate(R.layout.main_view, null);
windowManager.addView(mainView, getViewParam(true));
But I am getting these exceptions when I run this service.
07-12 09:16:34.087 12020-12020/production.kado.lock E/AndroidRuntime: FATAL EXCEPTION: main
Process: production.kado.lock, PID: 12020
java.lang.RuntimeException: Unable to create service production.kado.lock.services.LockService: android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class <unknown>
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3252)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1594)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #47: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.view.View.inflate(View.java:21008)
at production.kado.lock.views.KnowView.findAllViews(KnowView.java:115)
at production.kado.lock.views.KnowView.initView(KnowView.java:99)
at production.kado.lock.views.KnowView.<init>(KnowView.java:46)
at production.kado.lock.services.LockService.addLockView(LockService.java:87)
at production.kado.lock.services.LockService.onCreate(LockService.java:69)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3242)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1594)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 35: TypedValue{t=0x2/d=0x7f02012d a=-1}
I also used KnockView in activity but in activity, it works fine. The only problem is when used in service with WindowManager.
I figured out the problem I was using this
android:background="?selectableItemBackground"
property in my layout button after removing this it's working fine

Crash when creating view that relies on initialized singleton

I maintain a library that's causing a crash that's confusing me.
The library provides users with a custom View that extends FrameLayout. This custom View accesses a singleton (also defined by the library) in its constructors:
CustomView(Context context, AttributeSet attributeSet, int defStyleAttr) {
super(context, attributeSet, defStyleAttr);
// Other initialization.
mySingleton = MySingleton.getSharedInstance();
}
// Two-parameter and one-parameter constructors delegate to the three-parameter constructor above.
The method MySingleton::getSharedInstance throws an exception if the configuration method MySingleton::initSharedInstance has not previously been invoked:
private static void initSharedInstance(Application application) {
synchronized (MySingleton.class) {
if (sharedInstance == null) {
sharedInstance = new MySingleton(application);
}
}
}
public static MySingleton getSharedInstance() {
synchronized (MySingleton.class) {
if (sharedInstance == null) {
throw new IllegalStateException("You must call initSharedInstance before calling getSharedInstance.");
}
}
return sharedInstance;
}
I instruct library users to call MySingleton::initSharedInstance in their Application::onCreate method, and have confirmed with the users reporting this crash that they are doing so.
Here's a sample stack trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.activity.ManageCamerasActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #1: Error inflating class com.github.stkent.amplify.prompt.DefaultLayoutPromptView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #1: Error inflating class com.github.stkent.amplify.prompt.DefaultLayoutPromptView
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.m.b(SourceFile:288)
at android.support.v7.app.e.setContentView(SourceFile:140)
at com.example.activity.ManageCamerasActivity.onCreate(SourceFile:232)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
... 9 more
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class com.github.stkent.amplify.prompt.DefaultLayoutPromptView
at android.view.LayoutInflater.createView(LayoutInflater.java:657)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:966)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:843)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:631)
... 22 more
Caused by: java.lang.IllegalStateException: You must call initSharedInstance before calling getSharedInstance.
at com.github.stkent.amplify.b.a.b(SourceFile:101)
at com.github.stkent.amplify.prompt.a.<init>(SourceFile:113)
at com.github.stkent.amplify.prompt.DefaultLayoutPromptView.<init>(SourceFile:52)
at com.github.stkent.amplify.prompt.DefaultLayoutPromptView.<init>(SourceFile:44)
... 24 more
Clearly, MySingleton::getSharedInstance is being called before MySingleton::initSharedInstance, i.e. the CustomView constructor is being invoked before Application::onCreate, but I have no idea how that can be the case. I thought the Application instance would always be fully created before any Activity/Fragment/View code executed.
What's going on here?
N.B.: if links to more specific implementation details would be useful, please comment to that effect.
In getSharedInstance (Application a) do: if (instance == null) {
initSharedInstance (a); }

What in my Activity is causing this Android.Views.InflateException?

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>

Adding a custom view to Android Wear for canvas

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

Implement a custom TextView with custom textSize

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();
}

Categories

Resources