I have a weird problem. While running my Android application, I receive Exception:
java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RelativeLayout
the code, where I try to get relativeLayout
v = inflater.inflate(R.layout.home, null);
RelativeLayout btn = (RelativeLayout) v.findViewById(R.id.my_button);
in xml, I've got this:
<RelativeLayout
android:id="#+id/my_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button_selector"/>
While debugging, I've noticed that in v there is Button with mID equals to R.id.my_button.
Although, when I change id in xml and code to anything else, I receive NullPointerException.
Any ideas why this is happen ?
EDIT:
Maybe it will be helpful, if I add that in older version of that file there was Button, but was replaced with RelativeLayout. I'm using GIT. Project was cleaned many times and eclipse was restarted also.
Delete the R.Java file and once recreated run the app it will work fine.
The problem was that I've made one more layout file that I forgot about and it was for the same Activity byt different configuration. Problem solved.
Related
The problem is that when I rename the id of any view like a button in the xml file, the name of the view doesn't get updated in the activity while using view binding.
When I rename the id, I use (refactor) or (shift+f6).
This is a big problem because when ever I rename an id in the XML file, it will break the code in the activity in case I'm using View Binding.
Example:-
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
when I change Button1 to Button2, the code breaks in the activity in case I'm using View binding.
Can anyone give me any way to properly rename the IDs so that the names in the activity get updated automatically while using View Binding.
Please, remember I do use (refactor) not f2.
If you agree with me that this is a very urging problem and it breaks our code, please vote this question up, so it will be suggested to more android developers. Maybe some already found a solution.
In my android project none of my #string variables are not working.
For example in the title bar I see #string/app_name instead of myapp.
In my AndroidManifest.xml it looks normal, with the line android:label="#string/app_name"
Also, in my layout buttons and textviews show #string/action_sign_in but they also have the line in there xml layout. android:text="#string/action_sign_in"
Anyone had this issue before?
Clean, rebuild verify if its working.
If not, you can alt+enter on the #string/action_sign_in redline and click on create string value resource then set the value to the string.
I am working on an Android application that will be using images and so I am working being able to grab an image and display it. Though when I put in setContentView(R.layout.main); It tells me that main does not exist but offer to put into the layout file or R.java. When trying to place it into the file and save the R.java files tells me it reverted it right back to the original purpose, why is that?
From your comments you said you have activity_main.xml and not main.xml
So change to
setContentView(R.layout.activity_main);
ALso if you have imported R.java remove the same. Make sure there are no errors in your resource files. Clean and build.
If you have a imageview in activity_main.xml with id gimg1 then
ImageView iv = (ImageView) findViewById(R.id.gimg1);
and import
import android.widget.ImageView;
Its the same to initialize if you have others view's in your xml
R.java file is generated automatically, you can't edit it. In your case tyr to clean your project via Project->Clean
I'm trying to set a relative layout's background with,
relativeLayout1.setBackgroundColor(0x00000000);
My program keeps crashing though. Here's the logcat.
Code:
RelativeLayout window=(RelativeLayout) findViewById(R.id.window);
window.setBackgroundColor(0x00000000);
That's the only stuff apart from the regular code setContentView(R.layout.something); and super.onCreate(savedInstanceState);
Entire code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invisible);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
window=(RelativeLayout) findViewById(R.id.window);
window.setBackgroundColor(0x00000000);
}
From the logcat attached, I can say that most likely your window pointer is null at the time you are trying to set background color. It can be caused by different types of problems:
Your something.xml layout does NOT contain element with android:id="#+id/window" attribute
Your something.xml layout DOES contain element with android:id="#+id/window" attribute, but this element is not RelativeLayout
Your project resource data got messed up. Try to do Project->Clean to rebuild resources
Is there is your R.layout.something a layout with the id R.id.window ?
Maybe you've missed something?
You can use this instead
relative layout.setBackgroundDrawable(get resources().get drawable(R.drawable.bg);
Or you could define it in XML
android: background="#drawable/bg"
This was another of Eclipse's moments. It didn't compile my code, so I was executing the old code. Restarted Eclipse and my app works fine now.
I've looked at several other questions related, one answer even seemed like it solved the problem however I was wrong.
The issue is I get:
Parser exception for C:\Documents and Settings\djordan\My Documents\SampleTest\AndroidManifest.xml: The markup in the document following the root element must be well-formed.
When I drag/drop a new TextView or Multiline Text view item to the canvas. All other Text Fields add fine.
EDIT: Pasted the wrong error ... updated. and Added following work flow:
1. open SDK, check API 16 installed.
2. open Eclipse, create new Android project
- name, target/minimum API 16
3. in Graphical Layout drag "Plain Text" or "MultiLine Text" from the Text Fields.
4. view error above.
Flip over to the xml page, go through and find the textView you just added. Find the attribute where it says android:textSize="18" and change it to android:textSize="18dip" That should solve it. The error is telling you that the units for that attribute are not valid or were not set.