setContentView not working in my android code - android

I have an error in my android code..my setContentView is not working.
my code is setContentView(R.layout.secondlayout);
Error is secondlayout cannot be resolved or it is not a feild.

Make sure that you placed the secondlayout.xml file inside res -> layout folder. Also make sure there is no error in this file. If the below line is there in your import statements, remove it.
import android.R;

Try this
import com.example.appname.R
Then, in eclipse go to project>clean , choose your project and press OK. Then wait until it is done. The clean operation regenrates R.

Related

adt bundle in ubuntu doesn't work

I got some Errors when I use adt bundle in Ubuntu.
I attached Screenshots of them.
what's problem?
Try import R, then clean the project and again delete the import R;
And clean again.
If it doesn't work, you have some problem in your xml.
Second screen just click import R.
First screen the only reason why permission is denied could be because of rights. Go to this directory and set read-write permissions for everyone.
EDIT: Also you could try Menu Bar -> HELP->Install New Software and select position on List starting on Eclipse..... https then try update Eclipse.

Error in R.java when create new activity

i am trying to insert new activity in my android application,after inserting the new activity it gives error in the R.java file,
String class
public static final int =0x7f050009;
it erase the variable name,and don't allow to update variable name. Is this problem occurs because i inserted new activity in the application,or problem is other ?
R.java is generated by android automatically. You don't(shouldn't) write/modify it. Just delete it and then clean and build the project again.
Try to clean you project from Project>Clean.. and build again.
When you create a new activity and after adding it in android manifest xml file, then building an application in eclipse should re generates the R.java.
If 'Build Automatically' enabled in eclipse, then on saving manifest file itself will trigger re generating R.java. Don't do anything in R.java its automatically generated.
if u create the different packages for your project Like Activity, Fragment,Bean.
In such case R will show in red color at setContentView method.at that time keep cursor at R and press ALT + ENTER it will show for import R.
click on it..
it will resolve your problem.

Android tutorial error: R cannot be resolved to a variable

http://developer.android.com/training/basics/firstapp/building-ui.html
I have been following this tutorial, but I have two errors, both "R cannot be resolved to a variable". I have made android apps (easy ones) in the past, and I remember this problem being fixed by checking whether or not I have an import R statement (which I don't) and whether or not the project has been cleaned before being built again (I cleaned it and I still get the error). I am at a loss as to what to do. Thanks!
*I also want to mention I did see the thread of the same title with 170 hits, and the solution of "delete the import R statement" does not apply to my problem (I don't think)
Thanks again
All code is straight from the link above, but here it is for convenience
activity_my_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
MyFirstActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MyFirstActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_first);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_my_first, menu);
return true;
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="menu_settings">Menu Settings</string>
</resources>
edit: When I clean the project, I get this message in the Console:
[2012-06-29 11:12:38 - MyFirstApp] W/ResourceType( 6140): Bad XML
block: header size 91 or total size 0 is larger than data size 0
[2012-06-29 11:12:38 - MyFirstApp]
C:\Users\zhong\workspace\eclipse\MyFirstApp\res\menu\activity_my_first.xml:2:
error: Error: No resource found that matches the given name (at
'title' with value '#string/menu_settings').
edit: added a line to layout xml file <string name="menu_settings">Menu Settings</string>
Fixed cleaning project errors, but I still can't run the project from the two R errors.
I'm working my way through the same example, and had the same (or very similar) problem.
Finally I noticed that there was a tiny little red x on the manifest.xml.
Sure enough, it was complaining about this:
android:label="#string/title_activity_hello_world" >
So I added:
<string name="title_activity_hello_world">Hello World</string>
to strings.xml and now it works.
I had the same issue while following the tutorial.
Cleaning the project or cleaning the imports did not solve the problem.
How the problem was (simply) solved : I quit Eclipse and relaunched it.
2 possible things
The package name specified in the Android Manifest isn't the same as the in the Java files.
Or, your IDE hasn't generated the R.java file in the gen/ folder. Try building again (despite the error) and it will usually clear it up.
Basically the R.java file is generated for you and in the same package so you can refer to it as simply R.
I had android:text="#string/button_send"
Which gave the error: No resource found that matches the given name (at 'text' with value '#string/button_send').
I tried to solve the error using the tuturial. But the tutorial fails to tell me to write all the lines required one by one. So I miss to write "<string name="button_send">Send</string>" in the strings.xml.
After adding the previous line the error disappear! Which I believe is something similar to the original problem in this thread.
So if I had copied the whole lines from the given tutorial it may had not happened, but by typing our-self is how I think is the best way of learning.
I found the solution:
go to your project->res->menu than open the XML file there and delete this line:
android:title="#string/menu_settings"
I deleted it and it start to work,after 3 hours of the same warning in the problem section.
It even make sense when you read it, "at title with value #string/menu_settings"
Pleas confirm there is no error in
1- Project folder
2- Problem View of eclipse
3- Console
4- error log as in image
You have to make sure to
Clean your project Project-->Clean
The /res directory doesn't contain errors (some files will show up
with a red icon on the file explorer)
Your imports doesn't contain this line import android.R;
Check that in AndroidManifest.xml, the attribute package has the
correct value. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="your.correct.package.name"
This fixed it for me:
https://stackoverflow.com/a/3259974/1538785
Seems like eclipse likes to add an import line to java files for no reason. Go onto your java file, hit ctrl+shift+o and all the import statements will appear. Delete the line that says import andriod.R
Right click on the Project, select "Android Tools" > "Add Support Library" > "Android Support Library, revision 11" > "Install"
In my case, the problem was that I was on 64-bit Linux, and the required 32-bit libraries were not installed, meaning that the build tools could not generate R.java.
Running this command, then cleaning the project, fixed the problem:
sudo apt-get install ia32-libs
Make sure the executables (aapt etc.) in sdk/build-tools/android-xyz/* are executable if you are running the default download on your Linux box...
I had the same error trying to do the first tutorial. To get the app to run I edited MainActivity.java so it looked like this
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Must I import com.companyname.productname.R?

I'm getting R cannot be resolved error.
I read R cannot be resolved - Android error. But there android.R file.
My project compiles if .java files contain import com.companyname.productname.R statement.
If I change my productname in AndroidManifest.xml there's many errors saying package com.companyname.productname does not exist.
I tried to remove all import com.companyname.productname.R lines, but then I got package R does not exist errors on lines with R.*. How can I work around this? Thanks.
Update:
So answer is yes, I must import com.companyname.productname.R. I managed to compile project after changing productname in AndroidManifest and find-and-replace all import com.companyname.productname.R statements with new productname.
You custom R class is generated based on the package name in your AndroidManifest. So if you change it there, it will be newly generated with the new package name.
Therefore make sure that you import the correct one and remove all old imports.
If you R class is not generated, you should check the for XML errors in your resource files and/or your AndroidManifest. Fix the issues there and your R file should be generated again.

Getting error in R.java

I am getting error in R.java file.
The error is
"The type R is already Defined."
I have tried deleting the R.java and Cleaning the project.
step i have wrote if this situation raise ::
dont delete it, clean it.
goto Windows >>show view >>console and try to solve error?
goto Project >>click -> build automatically
remove import of R from import section
goto layout file and check that is there any id is same name ?if yes then change it
in layout file delete any single space,save and refresh and run.
Reason is that you have checked two Builders that try to generate the same class.
You just have to uncheck the Java-Builder from Project->Properties->Builders.
May be there are two builders for the project.check it in project->properties->builders. I too had the same problem.
Chk this out: Fairtec: Type R is already defined

Categories

Resources