android.content.res.Resources$NotFoundException, android.view.InflateException with ImageButton - android

I receive the error below, mostly on Android 4.1 and 4.4:
java.lang.RuntimeException: Unable to create service se.weblink.weblinkunified.services.FloatingWindowService: android.view.InflateException: Binary XML file line #68: Error inflating class <unknown>
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2406)
at android.app.ActivityThread.access$1700(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #68: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at se.weblink.weblinkunified.services.FloatingWindowService.addViewToWindow(FloatingWindowService.java:122)
at se.weblink.weblinkunified.services.FloatingWindowService.onCreate(FloatingWindowService.java:92)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2396)
... 10 more
The addViewToWindow function:
final LayoutInflater layoutInflater = LayoutInflater.from(this);
popupWindow = layoutInflater.inflate(R.layout.window_floating_call, null);
final WindowManager.LayoutParams params = setupLayoutParams();
final View.OnClickListener clickListener = getViewClickListener();
final View.OnTouchListener touchListener = getTouchListener(params);
popupWindow.setOnClickListener(clickListener);
popupWindow.setOnTouchListener(touchListener);
final View overlayView = popupWindow.findViewById(R.id.overlay_view);
overlayView.setOnClickListener(clickListener);
overlayView.setOnTouchListener(touchListener);
final View topContainer = popupWindow.findViewById(R.id.call_top_container);
buttonContainer = (LinearLayout) popupWindow.findViewById(R.id.call_bottom_container);
buttonContainer.addView(getActiveCallButtons());
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
final Resources resources = getResources();
topContainer.setBackgroundColor(sharedPreferences.getInt(Constants.THEME_COLOR_PRIMARY, resources.getColor(R.color.accent_color_default)));
setupCloseButton();
windowManager.addView(popupWindow, params);
popupWindow.setTranslationY(Tools.getDensityIndependentSize(-120, resources));
popupWindow.setVisibility(View.GONE);
And the xml file "window_floating_call":
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<RelativeLayout
android:id="#+id/call_top_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#color/accent_color_default"
>
<ImageView
android:id="#+id/contact_image"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_contact_picture"
/>
<TextView
android:id="#+id/contact_initial_label"
android:layout_width="55dp"
android:layout_height="55dp"
android:gravity="center"
android:layout_marginRight="10dp"
android:textSize="25dp"
android:fontFamily="sans-serif-light"/>
<TextView
android:id="#+id/incoming_call_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/contact_image"
android:textColor="#color/text_color_bright"
android:text="#string/incoming_call"
android:textSize="10sp"
android:fontFamily="sans-serif"/>
<TextView
android:id="#+id/contact_name_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/incoming_call_label"
android:layout_marginTop="4dp"
android:layout_toRightOf="#+id/contact_image"
android:textColor="#color/text_color_bright"
android:textSize="18sp"
android:fontFamily="sans-serif"/>
<TextView
android:id="#+id/contact_number_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/contact_image"
android:gravity="top"
android:layout_below="#+id/contact_name_label"
android:fontFamily="sans-serif-light"
android:textSize="14sp"
android:textColor="#color/text_color_bright"/>
<View
android:id="#+id/overlay_view"
android:layout_width="match_parent"
android:layout_height="54dp"/>
<ImageButton
android:id="#+id/close_button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_cancel"
android:alpha="0.7"
style="#style/Base.Widget.AppCompat.ActionButton"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/call_bottom_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/call_top_container"
android:orientation="vertical"
android:weightSum="1"
>
</LinearLayout>
I can't figure out what causes the error. The problem does not occur on Android 5.1. And I don't seem to get why this code wouldn't be able to run on Android 4.1, 4.2, 4.4.
I appreciate if someone could help me. Thanks in advance!
Forgot to include the android.content.res.Resources$NotFoundException error. But the exception says that the resource file is not a drawable. I have the image ic_action_cancel in all other drawable folders (drawable-hdpi, drawable-mdpi etc.) except the main drawable folder(drawable). I tried adding the .png file to the main folder but it did not work.

I've faced the same problem as yours.I firstly used my inflateUtils that write by myself.
The code is:
public static View inflate(int id) {
return View.inflate(getContext(), id, null);
}
And the getContext() method return a applicationContext. And it occurred InflateExcepition.
And now I change to use View.inflate(context, resId, null), and the context is my activity. And it worked fun now! hope that can help you.
sorry for my poor English!

Related

Error: " android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class <unknown> "

When working with 4.7 WXGA API 23, I am getting the error of "android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class "
but in higher versions, it is all working well, I have added the code and the xml layout for my issue, don't understand why it is happening so
Code where this error is occurring
#NonNull
#Override
public PostsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list_layout,
parent, false);
return new PostsHolder(v);
}
XML Layout for recyclerview Item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/shadow_300_50"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:padding="5dp"
android:orientation="horizontal"
>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/profile"
android:id="#+id/post_user_profile_image"
android:scaleType="centerCrop"
android:layout_marginLeft="4dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/post_item_username"
android:layout_marginLeft="5dp"
android:layout_marginTop="8dp"
android:textAlignment="textStart"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#android:color/black"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time"
android:id="#+id/post_item_time"
android:layout_marginLeft="257dp"
android:layout_marginRight="5dp"
android:layout_marginTop="8dp"
android:textAlignment="textStart"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#android:color/black"/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:id="#+id/post_item_title"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAlignment="textStart"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#android:color/black"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="post_item_Category"
android:textColor="#color/colorPrimaryDark"
android:textSize="13sp"
android:textStyle="bold"
android:id="#+id/post_item_Category"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post_item_SpinnerC"
android:textColor="#color/colorPrimaryDark"
android:textSize="13sp"
android:id="#+id/post_item_SpinnerC"
android:layout_marginLeft="7dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/post_item_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginRight="3dp"
android:padding="3dp"
android:text="Post Description"
android:textColor="#android:color/black"
android:textSize="14dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:padding="2dp">
<Button
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mark"
app:srcCompat="#drawable/ic_heart_normal"
android:background="#drawable/button"/>
<Button
android:id="#+id/commenting_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:text="Comment"
android:background="#drawable/button"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
The problem is that a constructor for a class for creating one of the widgets doesn't exist or can't be found. The LayoutInflater class (in the sdk, ie: Android\Sdk\sources\android-17\android\view\LayoutInflater.java) uses reflection(code introspection) on the widget name to build the widget object. In your case I would guess it is 'de.hdodenhof.circleimageview.CircleImageView', in my case it was 'androidx.appcompat.widget.Toolbar'.
Finding the problem is buried in several layers of exceptions, and only the (pretty useless) 'Binary XML file line #7: Error inflating class...' message appears. I set up Android Studio to break on the android.view.InflateException exception:
The 'ie' InflateException at the bottom contains a java.lang.reflect.InvocationTargetException, which was in turn caused by a android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f03008f a=-1} exception (in my case)
In the LayoutInflator code:
Lin 616 is where they embed the exception named 'e' in the InflationException. Using the 'Evaluate Expression' feature of AS, I called 'e.getStackTrace()' which shows where the error really occurred:
StackTrace line 2 shows that it was line LayoutInflater line 587 (seen with red background in code image above) that threw the exception. Looking up to stackTrace line 0, it is the java.lang.reflect.Constructor.constructNative method that threw the original exception.
LayoutInflator line 587 is was trying to instanciate androidx.appcompat.widget.Toolbar (for me) and was looking for a Toolbar constructor with 2 arguments:
1) Context: ref to activity in which the layout was being inflated
2) AttributeSet: the opaque XmlBlock object named 'attrs'
I found this out by using Expression Evaluator on 'this.mConstructorArgs' (seen in line 584 of the code).
So, for me, it could not find a constructor with the Toolbar(Activity, XmlBlock) signature. So I have to look into where Toolbar is defined in by configuration and find out what constructors it has.
The usual 'solution' people give as in this question, is to clear caches, reset support libraries, run with the latest dependencies etc, which don't get you to the root cause, and sometimes don't work.
I'm documenting this here somewhat for my own future problems -- and also as a plea to the Android developers to give clearer error messages. 'Binary XML file line #7: Error inflating' doesn't help much and line 7 of some Binary XML file doesn't give and clues.
#ramneek, 'de.hdodenhof.circleimageview.CircleImageView' is the only unusual widget you are inflating there must be some method or constructor for that class/widget that the inflator can't find.

android.view.InflateException: Binary XML file line #0: Error inflating class

Can anyone tell me whats wrong with this layout? Because I can't seem to find anything unusual that could cause InflateException. Also I am getting this exception mostly on Samsung Devices, others seem to work more or less fine. Full exception
Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{class name}: android.view.InflateException: Binary XML file line #0: Error inflating {class name}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/loginContainer"
style="#style/AuthenticationTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0">
<LinearLayout
android:id="#+id/holder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="#string/window_enter_identifier_greeting_label"
android:textAlignment="center"
android:textSize="26sp" />
<TextView
android:id="#+id/info"
style="#style/AuthenticationTheme"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="#dimen/authentication_margin_from_sides"
android:layout_marginRight="#dimen/authentication_margin_from_sides"
android:gravity="center"
android:text="#string/window_enter_identifier_phone_description_label"
android:textAlignment="center"
android:textSize="16sp" />
<LinearLayout
android:id="#+id/phoneInputHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.custom.authentication.views.IdentifierInputView
android:id="#+id/authenticationInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/authentication_margin_from_sides"
android:layout_marginRight="#dimen/authentication_margin_from_sides" />
</LinearLayout>
</LinearLayout>
<com.custom.authentication.views.BottomButtonsView
android:id="#+id/bottomButtons"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:linkText="#string/window_enter_identifier_login_with_email_button"
app:mainText="#string/Continue" />
</android.support.constraint.ConstraintLayout>
I am inflating this layout in my custom view class like this:
class EnterIdentifierView(context: Context) : ConstraintLayout(context) {
init {
inflate(context, R.layout.view_enter_identifier, this)
}
And creating my custom view like this:
override fun onCreateControllerView(layoutInflater: LayoutInflater, viewParent: ViewGroup) = EnterIdentifierView(layoutInflater.context)
I don't think this is an answer, more of a suggestion, but as I don't have enough reputation have to post it as such.
From my experience when I get this type of error it sometimes indicates an OutOfMemory error, check to make sure your custom classes aren't trying to load any large images etc.
Also check the full Stacktrace carefully, if you have access to one, for any OOMs

ClassCastException from View objects in android studio

In Android Studio, when i try to run my project i get a ClassCastException as following:
Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
when the View object is 100% RelativeLayout and not EditText.
The problem occurs on another View element if i comment out the above-related code..
I have tried cleaning and rebuilding the project, i have tried editing the XML file and change the ID, i have tried deleting the XML file and restarting Android Studio and then make a new XML file, all that to no avail.
Thank you for your help.
EDIT
When i add new views to the xml, with id that i never used, somehow the activity before it gets nulls on its own (and totally seperate) xml file.. this is weird because theres no reason why a simple view on a different xml file should impact another xml that is loaded even before it..
EDIT2
I inspected View rl = activityView.findViewById(R.id.notification); and found out that it gets etMessage which is another view element in the same XML and is infact an EditText type view.
The code before the exception occurs:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setInitialView();
}
private void setInitialView() {
RelativeLayout frame = (RelativeLayout) findViewById(R.id.activity_frame);
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View activityView = layoutInflater.inflate(R.layout.chat, null,false);
frame.removeAllViews();
frame.addView(activityView);
chatMap.put("Public", new ArrayList<ChatMessageBox>());
// move the notification layout outside the screen
RelativeLayout rl = (RelativeLayout) findViewById(R.id.notification);
TranslateAnimation moveAnim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
moveAnim.setDuration(0);
moveAnim.setFillAfter(true);
rl.startAnimation(moveAnim);
}
This activity's parent XML, which is loaded with setContentView in its onCreate:
<?xml version="1.0" encoding="utf-8"?>
<!-- Making the drawer layout the root view -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- MAIN CONTENT -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
<!-- The ListView to be displayed when the drawer layout is active -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
And the XML that has the appropriate view:
<?xml version="1.0" encoding="utf-8"?>
<!-- Making the drawer layout the root view -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="#+id/chat_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:id="#+id/scroller"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#+id/bSendMessage"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/chat_bg" >
<LinearLayout
android:id="#+id/chatWindowContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/bSendMessage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/ic_send" />
<EditText
android:id="#+id/etMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/bSendMessage"
android:layout_toLeftOf="#+id/bSendMessage"
android:paddingLeft="10dp"
android:background="#drawable/text"
android:textColor="#000000"
android:imeOptions="actionNone"
android:inputType="textMultiLine"
android:maxLines="4"
android:singleLine="true"
android:layout_alignParentRight="false"
android:layout_alignWithParentIfMissing="false" />
<!-- the desired element -->
<RelativeLayout
android:id="#+id/notification"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/bg_upload"
>
<TextView
android:id="#+id/tvNotification"
android:layout_gravity="center"
android:text="New Message Recieved"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="#000000" />
</RelativeLayout>
</RelativeLayout>
Error log:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.raad/com.example.raad.activities.ChatActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
at com.example.raad.activities.ChatActivity.setInitialView(ChatActivity.java:336)
at com.example.raad.activities.ChatActivity.onCreate(ChatActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) 
at android.app.ActivityThread.access$600(ActivityThread.java:165) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5391) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.system.NativeStart.main(Native Method)
The problem was probably with the cache of the application in the device itself.
I ran the application on a different device and there was no issue, so i cleared the data and cache from the device (Settings -> Apps) and uninstalled the app (just uninstalling didnt work), and now it works.
Change your onCreate like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayoutXmlName);
setInitialView();
}
Edit:
Try changing this line:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.notification);
to
RelativeLayout rl = (RelativeLayout) activityView.findViewById(R.id.notification);
Hope it helps you.
R.id.activity_frame
does not exists in your xml file. Try to resolve this.
And post the complete code.

java.lang.RuntimeException: Unable to start activity

I have just launched my First Android Application on the Play Store name Indian Logo Quiz
And I am receiving certain crash reports. Of which one is This :
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.indian.logoquiz/com.indian.logoquiz.Play}:
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java: 56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
at android.app.Activity.setContentView(Activity.java:1928)
at com.indian.logoquiz.Play.onCreate(Play.java:39)
at android.app.Activity.performCreate(Activity.java:5250)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:593)
... 23 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
at android.content.res.Resources.loadDrawable(Resources.java:2854)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3460)
at android.view.ViewGroup.<init>(ViewGroup.java:446)
at android.widget.LinearLayout.<init>(LinearLayout.java:176)
at android.widget.LinearLayout.<init>(LinearLayout.java:172)
... 26 more
My play.xml is as follows :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/playpage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<Button
android:id="#+id/lvl1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.33"
android:alpha="0.85"
android:text=""
/>
<Button
android:id="#+id/lvl2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.33"
android:alpha="0.85"
android:text=""
/>
<Button
android:id="#+id/lvl3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.34"
android:alpha="0.85"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<Button
android:id="#+id/lvl4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.33"
android:alpha="0.85"
android:text="" />
<Button
android:id="#+id/lvl5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.33"
android:alpha="0.85"
android:text="" />
<Button
android:id="#+id/lvl6"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.34"
android:alpha="0.85"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp">
<Button
android:id="#+id/lvl7"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.33"
android:alpha="0.85"
android:text="" />
<Button
android:id="#+id/lvl8"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.33"
android:alpha="0.85"
android:text="" />
<Button
android:id="#+id/lvl9"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.34"
android:alpha="0.85"
android:text="" />
</LinearLayout>
</LinearLayout>
The app is getting crashed on pressing level 1 button ....
Can it be because of nested weight in any case ??
Please help !....
You are getting Out of Memory Exception for Images in your Activity.
When any device with small heap or buffer size uses application it may get Memory exception because you may have used High resolution images.
try below code to reduce memory usage of application bitmaps.
private Bitmap decodeFile(File f){
Bitmap b = null;
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE /
(double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
return b;
}
Hope it Helps!!
Other answers have talked about Out of Memory Exception, but since you've talked about nested weight's here is something which might help in understanding that better. It might not solve your problem but clear few things.
In general there are two parameters which are used together many a times:
android:weightSum="1" and android:layout_weight. If you don't specify the weightSum in the outer layout, its considered to be 1. The sum of all android:layout_weight inside an outerlayout should be equal to the weight sum of that outer layout. Eg:
If you have 3 Buttons inside a LinearLayout and you want to define equal weights to them, then you can do it as follows.
<LinearLayout
...
android:weightSum="3"
>
<Button
...
android:layout_weight="1"
/>
<Button
...
android:layout_weight="1"
/>
<Button
...
android:layout_weight="1"
/>
</LinearLayout>
Similarly you can do for layouts:
<LinearLayout
android:weightSum="3"
>
<LinearLayout
...
android:layout_weight="1"
/>
<LinearLayout
...
android:layout_weight="1"
/>
<LinearLayout
...
android:layout_weight="1"
/>
</LinearLayout>
you forgot to put end tag for parent Layout in last. </LinearLayout>
It is good practice to remove Bitmaps from memory as soon as you do not need them.
A way you can do it by recycling the Bitmap:
Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
// Use here your newImage...
newImage.recycle();
newImage = null;

Same code works on phisical device but not in the Android 4 emulator

My app is already using the Google Maps Android API v2 and it is working on my Galaxy Mini (Android 2.3.6). When I try to test in on an emulator using Platform 4.0.3, API level 15 and Google APIs, a get the following error:
01-23 14:26:31.581: ERROR/AndroidRuntime(688): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.remotepark/br.com.remotepark.activities.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:262)
at br.com.remotepark.activities.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
... 11 more
Caused by: java.lang.NullPointerException
at br.com.remotepark.fragments.MapFragment.initLocation(MapFragment.java:86)
at br.com.remotepark.fragments.MapFragment.onCreateView(MapFragment.java:62)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:270)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
... 22 more
The line that throws the exception is this:
mMap.setInfoWindowAdapter(new MapInfoWindowAdapter(getLayoutInflater(savedInstanceState)));
The MapInfoWindowAdapter constructor is like this:
MapInfoWindowAdapter(LayoutInflater layoutInflater) {
mLayoutInflater = layoutInflater;
mWindow = mLayoutInflater.inflate(R.layout.map_info_window, null);
mContents = mLayoutInflater.inflate(R.layout.map_info_window, null);
}
And the map_info_window.xml is:
<ImageView
android:layout_width="40dip"
android:layout_height="40dip"
android:src="#drawable/ic_remote_park"
android:id="#+id/remotepark_logo"
android:layout_marginLeft="0dip"
android:layout_marginRight="8dip"
android:layout_marginTop="16dip"></ImageView>
<LinearLayout
android:id="#+id/balloon_inner_layout"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:paddingTop="5dip">
<LinearLayout
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="0dip">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:id="#+id/parking_name_info_window"
android:textSize="7pt"
android:singleLine="true"
android:textStyle="bold"
android:ellipsize="end"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="2dip">
<RelativeLayout
android:id="#+id/frameLayout1"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:id="#+id/availability_label_info_window"
android:text="#string/places_str"
android:textStyle="bold"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:id="#+id/availability_info_window"
android:layout_toRightOf="#+id/availability_label_info_window"
android:layout_marginLeft="5dip" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/price_label_info_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/price_symbol_info_window"
android:layout_toRightOf="#+id/price_label_info_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/price_symbol_str"
android:textColor="#FFF"
android:layout_marginLeft="5dip"/>
<TextView
android:id="#+id/price_info_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/price_symbol_info_window"
android:layout_marginLeft="2dip"/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="right"
android:layout_marginBottom="5dip">
</RelativeLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btn_more_info_disabled"
android:id="#+id/more_info_button"
android:layout_marginLeft="10dip"
android:layout_marginRight="8dip"
android:layout_marginTop="16dip"></ImageView></LinearLayout>
Do you have any idea why its not working?
According to Google, Google Play services API's require 'physical development device', so they are not officially supported on the emulators. So, if you are running on an emulator, it is almost certain you are missing the required services. People report getting it to work in emulators, but I have not tried myself. You can view than answers in this post, if you want to look at installing Google Play on an emulator.
You are getting an error when you try to inflate the MapFragment or SupportMapFragment from your xml. It is usually a good idea to check if the device has Google Play services by calling isGooglePlayServicesAvailable(), and make sure it returns ConnectionResult.SUCCESS before proceeding. This allows you to present an error dialog instead of just crashing. Google has even provided an error dialog for some othercodes, you can have them prompted to upgrade/install via getErrorDialog(), like --
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
switch (resultCode) {
case ConnectionResult.SUCCESS: // proceed
break;
case ConnectionResult.SERVICE_MISSING:
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
case ConnectionResult.SERVICE_DISABLED:
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 1);
dialog.show();
}
Do this before calling setContentView.

Categories

Resources