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.
Related
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="#string/general_dts_title"
style="#style/TextAppearance.Compat.Notification.Line2"
android:layout_alignParentBottom="false"
android:layout_alignBottom="#+id/imgToggle"
android:textSize="16sp" />
<Switch
android:id="#+id/imgToggle"
android:layout_width="42dp"
android:layout_height="28dp"
android:layout_gravity="right"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" />
</RelativeLayout>
StatusBar: Caused by: android.view.InflateException: Binary XML file line #338 in com.dts.dtsxultra:layout/view_notification_sample: Class not allowed to be inflated android.widget.Switch ...
Could you please help.
I tried removing background attribute, but still it is not working.
I have tried the above piece of code, without giving the style to the textview, it works great. The error that you are facing may not be because of the piece of code.
RemoveView only allows some layout and widget to inflate. android.widget.Switch as in the error you are getting is not supported by RemoteView. You can see the list of supported views here
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.
So, I have an android app to finish making for a project, the same one as I asked questions about here before but this time I'm having trouble with a completely different aspect. Since the app is about Pokémon, in one activity I have some TextViews, EditTexts and a button set up to make an IV calculator. If you're curious and don't know what "IVs" are in Pokémon, google it of course. Anyway, getting back on track...this image here I put together highlights my problem
Above the boxed line you see there is a ClassCastException thrown saying cannot cast from type TextView to EditText...I know what that means in a general sense but I cannot fathom why it is occurring here because as you can see in the relevant line of code (which i pasted into the image) the part of that line that involves casting is casting from View (return type of findViewById) to EditText. The arg R.id.EditText06 IS referring to an EditText in my activity so I don't see where the bad casting attempt is supposedly occurring. Just for a little clarification of the context of this, this is part of stuff coded into the OnClickListener of the button.
Apologies if this question seems perhaps incomplete as regards content shown about the problem but it is quite late for me (so much so that I'm going to bed upon posting this) and please do ask for me to post other stuff if you feel you need it to try to help me.
EDIT: here is my xml file for the activity in question. http://pastebin.com/g5B8d393
EDIT2: OK now this is getting worse :( My current setup is that I have a sort of dummy main activity with just a button to launch an activity. While testing I changed which one it was to launch as desired...until some time last night (and I really don't see what could've started this problem) it worked fine with my IVCalculatorActvity once I had it working fully and properly, and with the other activity. But now, it just crashes when I hit the button in the main activity...I'm just going to upload the project somewhere. (http://www.filehosting[DOT]org/file/details/429262/PokeUtility.zip)
Grrr...curse this reputation restriction on link posting >.>
I understand the reasoning but meh...
EDIT3: apologies for ssuch sudden editing but I only just saw you latest post now HalR. Testing app atm.
findViewById doesn't return an object of class View, but of whatever class that object is indicated in your XML file. Whatever you think you are describing your EditText06 as in your XML, it thinks its a TextView instead of and EditText. Double check and make sure you aren't defining the id="EditText06" in more than one spot.
If you show your xml, It would likely be easier for someone to point out the specific problem in your xml file.
You need to use the format "#+id/..." once for each label, the first time it shows up (either as a reference or as an id". Otherwise you generate multiple labels. I edited your file the way I think it needs.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".IVCalculatorActivity" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/calculate" />
<EditText
android:id="#+id/EditText01"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textViewD"
android:layout_alignBottom="#id/textViewD"
android:layout_alignLeft="#+id/EditText05"
android:ems="10" />
<EditText
android:id="#+id/EditText03"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView5"
android:layout_alignBottom="#id/textView5"
android:layout_alignLeft="#+id/EditText02"
android:ems="10" />
<EditText
android:id="#+id/EditText04"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#id/textView6"
android:layout_alignLeft="#+id/EditText03"
android:ems="10" />
<TextView
android:id="#+id/textViewA"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewC"
android:layout_alignParentTop="true"
android:text="#string/IVcalcHeader" />
<TextView
android:id="#id/textViewC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textViewD"
android:layout_below="#id/textViewA"
android:layout_marginTop="26dp"
android:text="Stat" />
<EditText
android:id="#id/EditText05"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/textViewC"
android:layout_alignBottom="#id/textViewC"
android:layout_alignRight="#id/textViewA"
android:ems="10" />
<TextView
android:id="#id/textViewD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewB"
android:layout_below="#id/EditText05"
android:layout_marginTop="27dp"
android:text="Stat value" />
<EditText
android:id="#+id/EditText02"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/textViewB"
android:layout_alignBottom="#id/textViewB"
android:layout_alignLeft="#id/EditText01"
android:ems="10" />
<TextView
android:id="#id/textViewB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textView5"
android:layout_below="#id/EditText01"
android:layout_marginTop="28dp"
android:text="Level" />
<TextView
android:id="#id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textView6"
android:layout_centerVertical="true"
android:text="EV count" />
<TextView
android:id="#id/textView6"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignRight="#id/button3"
android:layout_below="#+id/EditText03"
android:layout_marginTop="29dp"
android:text="Base Stat" />
<TextView
android:id="#+id/textViewE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textView6"
android:layout_below="#id/textView6"
android:layout_marginTop="40dp"
android:text="Nature" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/EditText04"
android:layout_alignTop="#id/button3"
android:text=" " />
<EditText
android:id="#+id/EditText06"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/textViewE"
android:layout_alignBottom="#id/textViewE"
android:layout_alignLeft="#id/textView7"
android:ems="10" />
</RelativeLayout>
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 declared a ImageView on my layout (XML) but when I try to grab it by the ID I'm getting a TextField!?! Then my code throw a ClassCastException.
Here is my layout code (splash.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"
style="#android:style/Theme.Light" android:layout_height="wrap_content" android:background="#drawable/bg">
<ImageView android:layout_height="wrap_content" android:layout_width="match_parent" android:src="#drawable/splash" android:layout_marginTop="20pt" android:id="#+id/splashLogo"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/app_name" android:id="#+id/splashTitle" android:textStyle="bold" android:textSize="#dimen/title_size" android:layout_gravity="center" android:lineSpacingExtra="#dimen/spacer" android:gravity="center"></TextView>
<TextView android:id="#+id/splashCopyleft" android:textSize="#dimen/version_size" android:lineSpacingExtra="#dimen/version_spacing" android:layout_width="wrap_content" android:gravity="center" android:layout_height="wrap_content" android:text="#string/app_version_info" android:layout_gravity="center" android:textColor="#color/version"></TextView>
</LinearLayout>
Here is my Java (Activity) code:
// imports
public class SplashActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// Grab a ClassCastException here
ImageView logo = (ImageView) findViewById(R.id.splashLogo);
// Never get here :'(
Log.i("GOT", logo.toString());
}
}
Sometimes Eclipse shifts resource ids. Try cleaning your project and building it again.
First, clean your project and then build it.
If the problem persists, close Eclipse and re-open it.