Problem with nesting layouts - android

I have a following situation in my xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/topbar"
>
<TableRow >
<TextView android:minWidth="280sp"
android:id="#+id/header"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/path_color"
android:paddingLeft="10px"/>
</TableRow>
</TableLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.solid.explorer.HorizontalPager
android:id="#+id/horizontal_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</org.solid.explorer.HorizontalPager>
<RelativeLayout
android:id="#+id/contextMenu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
>
<org.solid.explorer.ContextOption
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<org.solid.explorer.ContextOption
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<org.solid.explorer.ContextOption
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<org.solid.explorer.ContextOption
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<org.solid.explorer.ContextOption
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</RelativeLayout>
<LinearLayout
android:id="#+id/drag_view"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<ImageView
android:id="#+id/drag_icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<TextView
android:id="#+id/drag_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<!-- <ImageView
android:id="#+id/drag_icon1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone"
/> -->
</RelativeLayout>
</LinearLayout>
The problem is with the LinearLayout with id drag_view.
In program code, I am using ImageView with id drag_icon1 (it is being dragged by finger). Then everything is fine. But i want to add text to this icon so then I need to drag not the ImageView but the LinearLayout with ImageView and TextView inside, am I right?
But when I switch to the LinearLayout and comment the old ImageView (as in presented xml) the program fails at the setContentView method (second line of onCreate(), so further code has nothing common with this problem). WTF? If drag_icon1 is uncommented and used as dragged image then everything is fine, setContentView works (even with drag_view present).
Here is the stack trace:
E/AndroidRuntime(13517): FATAL EXCEPTION: main
E/AndroidRuntime(13517): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.solid.explorer/org.solid.explorer.Explorer}:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
E/AndroidRuntime(13517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
E/AndroidRuntime(13517): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
E/AndroidRuntime(13517): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
E/AndroidRuntime(13517): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
E/AndroidRuntime(13517): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(13517): at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime(13517): at android.app.ActivityThread.main(ActivityThread.java:4196)
E/AndroidRuntime(13517): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(13517): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(13517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(13517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(13517): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(13517): Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
E/AndroidRuntime(13517): at org.solid.explorer.Explorer.onCreate(Explorer.java:126)
E/AndroidRuntime(13517): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
E/AndroidRuntime(13517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
E/AndroidRuntime(13517): ... 11 more
It suggests that something wrong is with LayoutParams, but with drag_icon1 it works without problems.. I don't have any idea what is wrong.. Thank you for any suggestions

Assuming the layout XML you posted is the complete file, the problems I see are:
You need to have a single root element.
You close a LinearLayout at the bottom of the document that I don't see.
However, if this is true, I'm not sure how you could get it to work by uncommenting the ImageView.

Related

Butterknife Nullpointer exception in android 4.2

Using Butterknife in my current project; it works fine on Kitkat and lollipop but throws a nullpointer exception on jellybean (4.2).
Exception is thrown at this line (btnLogin is NULL)
btnLogin.setTypeface(FontsHelper.robotoRegular(this));
Activity Code
public class Login extends Activity {
#Bind(R.id.btnLogin)
FButton btnLogin;
#Bind(R.id.btnRegister)
FButton btnRegister;
#Bind(R.id.edtEmail)
EditText edtEmail;
#Bind(R.id.edtPassword)
EditText edtPassword;
#Bind(R.id.rlProgress)
RelativeLayout rlProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
System.out.println("Binding password" + edtPassword); //prints null
System.out.println("Binding "+btnLogin); //prints null
btnLogin.setTypeface(FontsHelper.robotoRegular(this));
}
}
The layout....
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:gravity="center">
<ImageView android:id="#+id/imageView1" android:layout_width="wrap_content"
android:layout_height="0dp" android:src="#drawable/lm_logo_with_text"
android:layout_weight="0.5" android:paddingLeft="#dimen/pad_40dp"
android:paddingRight="#dimen/pad_40dp"
android:gravity="center_horizontal|center_vertical"/>
<LinearLayout android:id="#+id/llContent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="0.5"
android:orientation="vertical" android:paddingLeft="#dimen/pad_40dp" android:paddingRight="#dimen/pad_40dp">
<EditText android:id="#+id/edtEmail" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Email"
android:drawableLeft="#drawable/ic_email" android:inputType="textEmailAddress" style="#style/txt_white">
<requestFocus />
</EditText>
<View android:background="#color/gray"
android:layout_width="fill_parent" android:layout_height="#dimen/divider" android:layout_marginTop="#dimen/pad_16dp" />
<EditText android:id="#+id/edtPassword" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Password"
android:drawableLeft="#drawable/ic_pwd" android:inputType="textPassword" style="#style/txt_white" />
<View android:background="#color/gray"
android:layout_width="fill_parent" android:layout_height="#dimen/divider" />
<View android:background="#color/gray"
android:layout_width="fill_parent" android:layout_height="#dimen/divider" android:layout_marginTop="#dimen/pad_16dp"
android:visibility="gone"/>
<EditText android:id="#+id/edtEmailUnused" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Email address"
android:drawableLeft="#drawable/ic_pwd" android:inputType="textEmailAddress" style="#style/txt_white"
android:visibility="gone"/>
<info.hoang8f.widget.FButton
android:layout_width="fill_parent" android:layout_height="50dp"
android:id="#+id/btnLogin"
android:textColor="#color/white"
fbutton:buttonColor="#color/picton_blue"
fbutton:shadowColor="#color/san_marino"
fbutton:shadowEnabled="true"
fbutton:shadowHeight="2dp"
fbutton:cornerRadius="2dp"
android:text="Login"
android:layout_marginBottom="#dimen/pad_10dp"
android:layout_marginTop="#dimen/pad_40dp"
android:padding="#dimen/pad_15dp"
/>
<info.hoang8f.widget.FButton
android:layout_width="fill_parent" android:layout_height="50dp"
android:id="#+id/btnRegister"
android:textColor="#color/white"
fbutton:buttonColor="#color/niagara"
fbutton:shadowColor="#color/san_marino"
fbutton:shadowEnabled="true"
fbutton:shadowHeight="2dp"
fbutton:cornerRadius="2dp"
android:text="Register"
android:layout_marginBottom="#dimen/pad_40dp"
android:layout_marginTop="#dimen/pad_5dp"
android:padding="#dimen/pad_15dp"
android:onClick="gotoHome"
/>
<Button android:id="#+id/btnFb" android:background="#drawable/btn_fb"
android:layout_width="fill_parent" android:layout_height="wrap_content" style="#style/btn" android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/rlProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white_45_percent"
android:clickable="true"
android:animateLayoutChanges="true"
android:visibility="gone"
xmlns:wheel="http://schemas.android.com/apk/res-auto">
<com.pnikosis.materialishprogress.ProgressWheel
android:id="#+id/progress_wheel"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
wheel:matProg_barColor="#555588FF"
wheel:matProg_progressIndeterminate="true" />
</RelativeLayout>
Stack trace
java.lang.RuntimeException: Unable to start activity ComponentInfo{lifemedico.array.com.lifemedico/lifemedico.array.com.lifemedico.ui.login.Login}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at lifemedico.array.com.lifemedico.ui.login.Login.onCreate(Login.java:52)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
java.lang.NullPointerException
at lifemedico.array.com.lifemedico.ui.login.Login.onCreate(Login.java:52)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
There are no issues what so ever on lollipop and kitkat, but the debug statements both print null in jelly bean. Can't think of any reasons this is failing, any help is highly appreciated.
Thanks
Edit:
Adding debug option to butterknife gave me this
Not found. Trying superclass android.support.v7.app.ActionBarActivityd
MISS: Reached framework class. Abandoning search.
Some search online pointed to proguard, but i am not using proguard and this is still a debug version.
Guess i was looking down the wrong hole; there is no programming/syntax/logic error. As i have mentioned i looked into proguard and realized my gradle has multiDexEnabled; and for some reason Butterknife functions are stripped out. All i had to do is set my App to extend MultiDexApplication and it worked like a charm; took me a day to figure it out.
public class App extends MultiDexApplication {
I found a lead in this post https://github.com/JakeWharton/butterknife/issues/311
this issue can be resolved by updating your gradle file as mention by Butterknife on their gitgub
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
For exact reference you can refer link https://github.com/JakeWharton/butterknife
Please make your fields public i.e.:
#Bind(R.id.btnLogin)
public FButton btnLogin;
#Bind(R.id.btnRegister)
public FButton btnRegister;
#Bind(R.id.edtEmail)
public EditText edtEmail;
#Bind(R.id.edtPassword)
public EditText edtPassword;
#Bind(R.id.rlProgress)
public RelativeLayout rlProgress;
Butterknife cannot bind private variables, so make it public

App crash when xml layout is loaded

I've created a layout of a Fragment.A t the first time I've created it works without problem, but when I made some changes the app crash when I open it.
The very strange thing is that even if when I discard the changes ,and the xml code was perfectly equal to the first I've created, the app crash when the Fragment is open! So I don't understand what is the problem, is it possible an Eclipse's bug?
This is my code:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.lp.lemiediete.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/bmi_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#4285f4"
android:textSize="30dp"
android:text="Il tuo Bmi" />
<TextView android:id="#+id/bmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bmi_text"
android:layout_centerHorizontal="true"
android:gravity="center|center_vertical"
android:textColor="#000000"
android:textSize="50dp"
android:text="" />
<TextView android:id="#+id/tipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bmi"
android:textColor="#000000"
android:layout_centerHorizontal="true"
android:gravity="center|center_vertical"
android:textSize="20dp"
android:text="" />
<TextView android:id="#+id/peso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tipo"
android:textColor="#000000"
android:textSize="40dp" />
<TextView android:id="#+id/altezza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tipo"
android:layout_alignParentRight="true"
android:textColor="#000000"
android:textSize="40dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Logcat:
04-23 14:37:19.678: E/SQLiteLog(899): (1) no such table: date
04-23 14:37:19.693: D/AndroidRuntime(899): Shutting down VM
04-23 14:37:19.693: D/AndroidRuntime(899): --------- beginning of crash
04-23 14:37:19.735: E/AndroidRuntime(899): FATAL EXCEPTION: main
04-23 14:37:19.735: E/AndroidRuntime(899): Process: com.lp.lemiediete, PID: 899
04-23 14:37:19.735: E/AndroidRuntime(899): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lp.lemiediete/com.lp.lemiediete.MainActivity}: android.database.sqlite.SQLiteException: no such table: date (code 1): , while compiling: SELECT data FROM date
I'm not sure that it can be important, try checking taking(on internet too) a working code and check if missing anything. Looklike : tools:context= ".MainActivity or another and similar thing."
Other check if you have references in yor code (or in your control) of that at run not find.
I dont' know if I can help you,
good work!

IllegalArgumentException: No view found for id - on app restart

My app is built using fragments and the v4 support fragment manager. The app works well from startup and in fact I can't recreate this bug myself, so in principle the structure of my xml is fine. However my crash reports are indicating a lot of users are getting a crash when returning to the app, resuming it after a pause. I get:
IllegalArgumentException: No view found for id 0x7f06002d
Please refer to my XML below. The id reported as not found can vary, it can be any one of the nested RelativeLayouts. Why are these views disappearing/not found, randomly?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F7F5F5"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/menuLayout"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_above="#+id/bottomFragmentLayout"
android:layout_alignParentLeft="true"
android:visibility="visible" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/whenLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#+id/bottomFragmentLayout"
android:layout_toRightOf="#+id/menuLayout"
android:visibility="invisible" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/howLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#+id/bottomFragmentLayout"
android:layout_toRightOf="#+id/menuLayout"
android:visibility="invisible" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/whereLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomFragmentLayout"
android:layout_toRightOf="#+id/menuLayout"
android:visibility="invisible" >
</RelativeLayout>
<!-- The bottom bar -->
<RelativeLayout
android:id="#+id/bottomFragmentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#999999" >
</RelativeLayout>
</RelativeLayout>
Edit: Full stack trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.android/com.myapp.android.activity.MainFragmentActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f060030 (com.myapp.android:id/howLayout) for fragment WhatFragment{4220b058 #4 id=0x7f060030 WhatFragment}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060030 (com.myapp.android:id/howLayout) for fragment WhatFragment{4220b058 #4 id=0x7f060030 WhatFragment}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
at com.myapp.android.activity.MainFragmentActivity.onStart(MainFragmentActivity.java:449)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
at android.app.Activity.performStart(Activity.java:5114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java)
// try this way
1. when you gave other layout reference like android:layout_above="#+id/bottomFragmentLayout" so here "+" sign removed from reference "+" is for new id declaration so we not declare new id but just gave already define id reference.
2. in your xml define bottom layout at last and try using this layout id before bottom layout declaration that y it's not finding such kind of layout so you have decalre first layout then gave reference as another layouyt which i correct in my ans.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F7F5F5"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/bottomFragmentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#999999" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/menuLayout"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_above="#id/bottomFragmentLayout"
android:layout_alignParentLeft="true"
android:visibility="visible" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/whenLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#id/bottomFragmentLayout"
android:layout_toRightOf="#id/menuLayout"
android:visibility="invisible" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/howLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#id/bottomFragmentLayout"
android:layout_toRightOf="#id/menuLayout"
android:visibility="invisible" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/whereLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottomFragmentLayout"
android:layout_toRightOf="#id/menuLayout"
android:visibility="invisible" >
</RelativeLayout>
</RelativeLayout>

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