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
Related
The issue I am having is basically what is found in this answer but I will still explain below. I am testing on a device with android 5.0.1 and another with 4.2.2. On my device with 5.0.1 I get no errors and everything behaves as expected. On my 4.2.2 device I get the following error:
java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
at android.support.v7.widget.CardViewEclairMr1.getShadowBackground(CardViewEclairMr1.java:159)
at android.support.v7.widget.CardViewEclairMr1.getMinWidth(CardViewEclairMr1.java:150)
at android.support.v7.widget.CardView.onMeasure(CardView.java:181)
at android.view.View.measure(View.java:15696)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2261)
at android.view.View.measure(View.java:15696)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2225)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1290)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1527)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1190)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4860)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:766)
at android.view.Choreographer.doCallbacks(Choreographer.java:575)
at android.view.Choreographer.doFrame(Choreographer.java:542)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:751)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
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:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(Native Method)
with the error occurring at this line: card1.setBackgroundColor(getResources().getColor(R.color.White));
Now as suggested by the linked answer I have tried using setCardBackgroundColor however this causes the error to be produced on both my devices. Any suggestions?
The CardView I am attempting to use is a android.support.v7.widget.CardView
This is the layout producing the error:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#bbbbb5"
android:orientation="vertical"
android:weightSum="100">
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_gravity="center_vertical|top"
android:layout_marginBottom="50dp"
android:layout_marginTop="-65dp"
android:layout_toRightOf="#+id/prem_BACK"
android:layout_weight="0"
android:clickable="true"
android:paddingBottom="0dp"
android:src="#drawable/plane1" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignEnd="#+id/imageView"
android:layout_alignRight="#+id/imageView"
android:layout_marginBottom="40dp"
android:layout_marginRight="5dp"
android:src="#drawable/ic_navigate_next_white_48dp" />
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="280dp"
android:clickable="true"
android:onClick="card1ClickMethod"
card_view:cardCornerRadius="4dp">
<TextView
android:id="#+id/choice_text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:onClick="card1ClickMethod"
android:text="Loret ipsum sample"
android:textColor="#212121" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="340dp"
android:clickable="true"
android:onClick="card2ClickMethod"
card_view:cardCornerRadius="4dp">
<TextView
android:id="#+id/choice_text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:onClick="card2ClickMethod"
android:textColor="#212121" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="400dp"
android:clickable="true"
android:onClick="card3ClickMethod"
card_view:cardCornerRadius="4dp">
<TextView
android:id="#+id/choice_text3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:onClick="card3ClickMethod"
android:textColor="#212121" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view4"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="460dp"
android:clickable="true"
android:onClick="card4ClickMethod"
card_view:cardCornerRadius="4dp">
<TextView
android:id="#+id/choice_text4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:onClick="card4ClickMethod"
android:textColor="#212121" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/resulttext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/progressBar1"
android:layout_alignParentStart="true"
android:layout_gravity="center_vertical|left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:textSize="15dp" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/fab"
android:layout_gravity="right|center_vertical"
android:layout_marginTop="20dp" />
<ImageView
android:id="#+id/prem_BACK"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:src="#drawable/background4"
android:visibility="gone" />
After reading through the issue described here I noticed that it has since been closed. As far as I am aware I am using the most updated version of the platform tools and sdk tools and I still have this issue.
After some further testing, I have discovered that the emulator I am using does not have GooglePlayServices. I do not currently have access to a physical device with less than Android 6.0 to test on. Is it a possibility that not having GooglePlayServices could cause this issue?
This is working for me for cardview inside recycleview
((CardView)viewholder.itemView).setCardBackgroundColor(Color.WHITE);
You could also keep both versions of android happy by giving them what they want rather than trying to force and cast something:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
// below lillipop
view.setCardBackgroundColor(Color.WHITE);
// working for lower questions as per another answer posted here
} else {
// lollipop and above
card1.setBackgroundColor(getResources().getColor(R.color.White));
// known to be working for lillipop as per your question
}
Basically call different and working methods for each of the android versions.
To correctly identify android versions above and below lollipop use LILLIPOP_MR1 (Soruce)
You can use:
card1.setBackgroundColor(Color.rgb(255, 255, 255));
instead of:
card1.setBackgroundColor(getResources().getColor(R.color.White));
The easiest way to do what you want is using color filter, I guess. I tried this on 4.2.2 and it works fine.
cardView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
By using setBackgroundColor(int color) you are actually calling a method on View, switching the background. The CardView expects a RoundRectDrawableWithShadow, which you did just overwrite. Hence the exception.
CardView view = (CardView) findViewById(R.id.card);
view.setCardBackgroundColor(Color.RED);
Be sure to call cardView.setCardBackgroundColor(int) and it works. It works on 4.2.2, and by looking through the cardView source code I'm confident about other apis as well. If this also leads to a crash, it must be a different reason.
Probably this resolved by using PublicRoundRectDrawableWithShadow.
Create PublicRoundRectDrawableWithShadow.
package android.support.v7.widget;
import android.content.res.Resources;
public class PublicRoundRectDrawableWithShadow extends RoundRectDrawableWithShadow {
public PublicRoundRectDrawableWithShadow(Resources resources, CardView cardView, int backgroundColor) {
super(resources, backgroundColor, cardView.getRadius(), cardView.getCardElevation(), cardView.getMaxCardElevation());
}
}
And setBackground() like this.
final CardView cardView = (CardView) findViewById(R.id.card_view);
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
cardView.setBackgroundColor(Color.RED);
} else {
cardView.setBackgroundDrawable(new PublicRoundRectDrawableWithShadow(getResources(), cardView, Color.RED));
}
Please try below specific line to set background color of card view object:
cardView.setCardBackgroundColor(Color.WHITE);
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>
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.
I have been tweaking my main xml layout, adjusting the different views' positions in my relative layout, and suddenly when I started it on my emulator, it crashed with the message:ClassCastException
My XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<com.example.bingbong.Render
android:id="#+id/render"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#color/pattern5"
android:clickable="true" />
<TextView
android:id="#+id/fps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="fpasds"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/score_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="asd"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/score_bot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="asfa"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
The error log:
10-30 20:25:33.266: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bingbong/com.example.bingbong.Main_Activity}: java.lang.ClassCastException: android.widget.TextView
...
10-30 20:25:33.266: E/AndroidRuntime(269): Caused by: java.lang.ClassCastException: android.widget.TextView
10-30 20:25:33.266: E/AndroidRuntime(269): at com.example.bingbong.Main_Activity.onCreate(Main_Activity.java:26)
The Main_Activity - where line 26 is :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_layout);
r = (Render) findViewById(R.id.render); // <--- Line 26.
r.setView((TextView)findViewById(R.id.fps),(TextView)findViewById(R.id.score_top),(TextView)findViewById(R.id.score_bot));
}
I get this occasionally if I am moving around views or changing the types of views (ie, a TextView change to a Button, or something like that). What usually fixes it for me is to do a clean build.
In the "Project" menu in Eclipse, select "Clean..." and either do all projects or select that particular project.
Yo need clean the project, I got this error frequently
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.