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
Related
I just created a new projet, building some layouts, writing some code. Everything was fine except I got this error as shown in the picture below.
I tried to remove and type it again and when typing 'R.id.', there was no 'container' showing up in the list. So that means theres no problem with my R file, just that the 'container' word lost somewhere.
Those code above are actually default code in onCreate() method in your main activity everytime you create a new project in Android Studio. (I actually tried to create a new project to compare and yes it's the same, and without error as it's brand new.).
All I did was comment out the if statement block so the error goes away, and the application has run just fine on the emulator. I just don't know why I got that problem and how I can handle it as I may need to do that in the future. Thank you!
Edit after #Raghunandan has mentioned: Below is my xml.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/provide_information"
android:id="#+id/provideInfoButton"
android:layout_alignParentTop="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/take_picture"
android:id="#+id/takePictureButton"
android:layout_below="#+id/provideInfoButton"
android:layout_alignParentLeft="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/takePictureButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/classNameTextView"
android:layout_above="#+id/personNametextView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginBottom="38dp"
android:layout_above="#+id/emailTextView"
android:layout_alignLeft="#+id/emailTextView"
android:id="#+id/personNameTextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/emailTextView"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/classNameTextView"
android:layout_marginBottom="82dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/send_button"
android:id="#+id/sendButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
The reason is that I changed the root layout to RelativeLayout (the default of Android Studio was FrameLayout which has an id of "container"). My bad :). Thanks for mentioning about posting the xml file. I have not noticed that as I'm not as familiar with Android Studio as with Eclipse yet.
I can't fix this problem on my listview template: i have the error as in the title of my post, but i won't cast imageview to textview. Here's my code:
<?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="200dip"
android:layout_weight="1"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/mq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Metri quadri"
android:textColor="#33b5e5"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/citta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/mq"
android:layout_alignBottom="#+id/mq"
android:layout_centerHorizontal="true"
android:text="Citta"
android:textColor="#33b5e5"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/prezzo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Prezzo"
android:textColor="#e1e1e1"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/nome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="5dip"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="#string/testolungo"
android:textColor="#e1e1e1"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/foto"
android:layout_width="80dip"
android:layout_height="60dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="Release"
android:padding="5dip"
android:scaleType="centerCrop"
android:src="#drawable/stub" />
<TextView
android:id="#+id/descsplash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Small Text"
android:textSize="16sp"
android:textStyle="italic" />
</RelativeLayout>
The error for the java code is on line 58 of my file, where i assign the TextView id to TextView mq:
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.listlay, null);
TextView nome=(TextView)vi.findViewById(R.id.nome);
TextView mq=(TextView)vi.findViewById(R.id.mq); // here's the error (???)
TextView citta=(TextView)vi.findViewById(R.id.citta);
TextView prezzo=(TextView)vi.findViewById(R.id.prezzo);
ImageView image=(ImageView)vi.findViewById(R.id.foto);
TextView descrizione = (TextView)vi.findViewById(R.id.descsplash);
nome.setText(data.get(position).getNome());
mq.setText(data.get(position).getMetriQuadri());
citta.setText(data.get(position).getCitta());
prezzo.setText(data.get(position).getPrezzo());
descrizione.setText(data.get(position).getDescrizione());
imageLoader.DisplayImage(data.get(position).getForoUrl(), image);
And here is the logcat:
06-25 16:08:32.497: D/Debug(14642): Prendo Textview MQ
06-25 16:08:32.497: D/AndroidRuntime(14642): Shutting down VM
06-25 16:08:32.497: W/dalvikvm(14642): threadid=1: thread exiting with uncaught exception (group=0x40a561f8)
06-25 16:08:32.497: E/AndroidRuntime(14642): FATAL EXCEPTION: main
06-25 16:08:32.497: E/AndroidRuntime(14642): java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
06-25 16:08:32.497: E/AndroidRuntime(14642): at com.prova.listview.LazyAdapter.getView(LazyAdapter.java:58)
Does anyone know how i can fix this problem?
Eclipse tends to mess up your resources every now and then.
This leads to some odd behavior such as strings and images being swapped all over your app, and more commonly classCastException(s), which happen when Eclipse switches your Views' ids around.
A few solutions to that problem:
Clean your project.
Modify an xml layout file and save.
Delete your R file. (Don't worry it will be automatically generated again).
Basically anything that makes your project rebuild and re-generate the R file.
Just clean up your project and everything will be fine.
Some times this problem shows because we have created ImageView But Cast with Image Button as like this
<ImageView
android:id="#+id/iv_thumb"
android:layout_width="50dip"
android:layout_height="50dip"
/>
holder.imageView = (ImageButton) convertView.findViewById(R.id.iv_thumb);
we have declared Imageview but cast to ImageButton, for this reason also have face this problem.
Initially, I was getting error:
TextView : android:id="#+id/add_expected_seekbarvalue"
SeekBar : android:id="#+id/add_expected_seekbar"
As a fix, I renamed the id for TextView as
TextView : android:id="#+id/add_expected_valueseekbar"
Even clean up and other techniques didn't work but finally renaming the id worked like a charm.
for Visual Studio Xamarin.Android Development
Like mentioned by many, Cleaning the project did the trick for me on Visual Studio
For my Xamarin.Forms app, it happened after pulling in changes with new library reference.
Clean your project.
Basically anything that makes your project rebuild and re-generate the Rosource.Designer.cs file. (Burrowed from #Benito Bertoli)
just clean your project...
I also gone through similar problem..
after clean project everything will be fine..best luck
Maybe you have an image view in your .xml file and a button which dynamically generated and given an id in your java code and these two randomly get a same id and because of that you get this error.
try to change the id which you have given in your code and it will be fine.
I'm developing an Android 3.1. and above application.
On one activity I generate its layout dynamically. I'm using formdetail.xml as contentview:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.formdetail);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
currentFormId = extras.getString("FormId");
}
}
formdetail.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
downloadFormsButton and formErrorMsg must be always in form.
But using that xml I get this warning:
This RelativeLayout layout or its LinearLayout parent is useless
I need linearLayout to add TableLayouts programmatically.
How can I solve this warning?
Lint is correct in it's warning, your LinearLayout isn't doing anything useful, since its only child is a RelativeLayout. Remove the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" />
</RelativeLayout>
You either ignore it or right click your project. Click Build Path->Configure Build Path.... Under Android Lint Preferences look for UselessParent and set it's severity to ignore or click Ignore All.
EDIT:
I take that last part back. I think Ignore All will wipe all Lint warnings and errors.
I was also having the same issue with my app layout.
I don't know how and it is right way or not, but this get resolved after setting background attribute to both layouts.
Now there is no warning and working perfect. Hope it will also work for you.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#drawable/bg"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg2"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/img_BookImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:contentDescription="#string/India"
android:src="#drawable/india" />
</FrameLayout>
</FrameLayout>
That is just an warning from lint. Lint doesn't know you would later append views to this layout and it will warn you that you added an unnecessary extra view(increasing your layout depth).
You should just ignore the warning(If it really bothers you here is a link on how to remove warnings from lint).
I can't understand why findViewById returns the wrong element, here is the class:
public class EventDetailsFragment extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_details);
Log.d("First", findViewById(R.id.tuxtView1).getClass().toString());
Log.d("Second", findViewById(R.id.tuxtView2).getClass().toString());
Log.d("Third", findViewById(R.id.imageView1).getClass().toString());
}
}
And here is the xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="0dp"
android:layout_height="175dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="#fff"
android:gravity="right" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_event_image" />
<TextView
android:id="#+id/tuxtView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/tuxtView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tuxtView2"
android:layout_toRightOf="#+id/imageView1"
android:textColor="#555"
android:textSize="20dp" />
</RelativeLayout>
</LinearLayout>
The logcat ouput is:
12-26 23:43:20.249: D/First(6789): class android.widget.TextView
12-26 23:43:20.249: D/Second(6789): class android.widget.ImageView
12-26 23:43:20.249: D/Third(6789): class android.widget.TextView
So the point is, why do I get a imageview with id R.id.tuxtView2 and a textview with id R.id.imageView1. The application crashes if I want to assign a text value to R.id.tuxtView2, casted as a TextView.
Try to reload your project, maybe something wrong with R.java, or verify you are calling the good file with setContentView.
By refreshing/cleaning your project the R.java file will be reloaded and will find the named widgets.
I copied your code and made a dummy app. I got these results:
12-27 00:03:48.332: D/First(9165): class android.widget.TextView
12-27 00:03:48.332: D/Second(9165): class android.widget.TextView
12-27 00:03:48.332: D/Third(9165): class android.widget.ImageView
Apparently there is nothing wrong with the code
I've tried almost everything here, but nothing worked until I added a new component to the activity and, after running, noticing that the new component didn't show. It suggested my app wasn't updating with the runs. It worked by manually uninstalling the old version of the app an then running again.
I m trying to integrate the facebook in my app.I am using facebook example app downloaded from facebook as a reference(also generated the appId and all this process on developer.facebook).I am little lazy so I do not want to go through all the code of facebook sdk.In simple words i just add the com_android_facebook library project in my app, and also copy all the classes in facebook example app in my app except Example.java
Now i modify the MyActivity class look like Example.java class, means MyActivity now have all the code of Example class and also my main Activity.
and i change the layout of my app as
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="22px"
android:textColor="#ff00ff"
android:gravity="center"
>
</TextView>
<com.android.facebook.LoginButton
android:id="#+id/login"
android:src="#drawable/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<Button android:id="#+id/uploadButton"
android:text="#string/upload"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:layout_margin="20dp"
/>
<Button android:id="#+id/requestButton"
android:text="#string/request"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:layout_margin="20dp"
/>
<Button android:id="#+id/postButton"
android:text="#string/post"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:layout_margin="20dp"
/>
<Button android:id="#+id/deletePostButton"
android:text="#string/delete"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:layout_margin="20dp"
/>
</RelativeLayout>
Now when i run my app i am getting there errors
10-14 00:58:37.786: ERROR/AndroidRuntime(3971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.myapp/com.android.myapp.MyActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class com.android.facebook.LoginButton
10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class com.android.facebook.LoginButton
10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: java.lang.ClassNotFoundException: com.android.facebook.LoginButton in loader dalvik.system.PathClassLoader#44c06850
Any help, suggestions are most welcome...
I also faced the same issue. I changed the main.xml from:
<com.facebook.android.LoginButton
android:id="#+id/login"
android:src="#drawable/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
/>
to (Current package is com.facebook.fbtest_simple):
<com.facebook.fbtest_simple.LoginButton
android:id="#+id/login"
android:src="#drawable/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
/>
As LoginButton is not part of FB standard library.
As of version 3.0 of the Facebook SDK, the LoginButton is now available as part of the SDK as
com.facebook.widget.LoginButton
See https://developers.facebook.com/docs/reference/android/3.0/LoginButton
Note that Facebook's own upgrade instructions here reference the wrong package, the LoginButton must be declared as com.facebook.widget.LoginButton, not com.facebook.LoginButton
There is no LoginButton in the main source of that SDK.
It's available in the sample code only, so it just fails to load the unavailable class (ClassNotFoundException)...
Easy one :
10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: java.lang.ClassNotFoundException: com.android.facebook.LoginButton in loader dalvik.system.PathClassLoader#44c06850
Looking for in the facebook src , you can go to this path:
AsyncFacebookRunner.java , Facebook.java, FacebookError.java ,
FbDialog.java, Util.java
..And see that there isn't nothing like LoginButton , then i can think that you are using an old tutorial or something like that.
Initializing the Facebook SDK is what worked for me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_login);
}