0

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" />

  • Without showing further code it's almost impossible to give an accurate answer. Post the code where you call `setContentView(...)` and also post your XML layout file(s). – Squonk Jun 02 '13 at 09:11
  • Doing the above you'll need to assign an id to your layout. Add this to your .xml file android:id="@+id/layout1" – Katana24 Jun 02 '13 at 09:23
  • [This question](http://stackoverflow.com/questions/1851633/how-to-add-button-dynamically-in-android) is looking same as your. And make sure you are having this code in `onCreate()` of Activity. – Jagdeep Singh Jun 02 '13 at 09:12

2 Answers2

1

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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I didn´t declare it :(. Where do I have to declare it and how do I do it ? –  Jun 02 '13 at 09:12
  • in the same Layout you pass as parameter to setContentView – Blackbelt Jun 02 '13 at 09:15
  • @blackbelt : Don't advise people to import their own package's R.java. It is done implicitly. – Squonk Jun 02 '13 at 09:25
  • @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 – Blackbelt Jun 02 '13 at 09:28
  • @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. – Squonk Jun 02 '13 at 20:36
0

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.

Rahul
  • 10,457
  • 4
  • 35
  • 55