I want to create a new button. This code is in my MainActivity.
public void method1 (View view)
{
Button myButton = new Button(this);
myButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
layout.addView(myButton);
}
I get an error on R.id.layout, saying layoutcan ́t be resolved or is not a field. How can I fix it? I am a newbie on Android.
//Edit my acitvity_main.xml looks like this
<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=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:onClick="neuerTrainingsplan"
android:text="@string/neuerPlan" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toLeftOf="@+id/button2"
android:onClick="TrainingsplanAbrufen"
android:text="@string/TrainingsplanAbrufen" />
2 Answers 2
The error means that you haven't declared a layout with id layout1. If you have check the import from your class in order to import the correct R class. Should be yourpackage.R not android.R
answered Jun 2, 2013 at 9:04
Blackbelt
158k31 gold badges308 silver badges310 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Blackbelt
in the same Layout you pass as parameter to setContentView
Squonk
@blackbelt : Don't advise people to import their own package's R.java. It is done implicitly.
Blackbelt
@Squonk that`s not always true. Sometimes eclipse import the android.R one, and I see many times people asking why thy can not references theirs id, when they actually declared it
Squonk
@blackbelt : I wasn't talking about a scenario where
android.R has been imported (which should not be done). What I meant is that is not necessary to explicitly import R.java for your own package. You may need to do it if you are using different packages but any Android class which is in the main package will implicitly have access to the R classes without the need to import it and there can be problems if it is explicitly imported.Make sure you made the linear layout in your xml file. And its android:id is layout1.
<LinearLayout android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Add this is in your layout file.
answered Jun 2, 2013 at 9:31
Rahul
10.6k5 gold badges37 silver badges56 bronze badges
5 Comments
Rahul
in your layout file. that u use in setcontentview(layoutfile.xml)
Rahul
use android:orientation="vertical"
default
setContentView(...)and also post your XML layout file(s).onCreate()of Activity.