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" />
asked Jun 2, 2013 at 9:01
3
  • 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). Commented Jun 2, 2013 at 9:11
  • This question is looking same as your. And make sure you are having this code in onCreate() of Activity. Commented Jun 2, 2013 at 9:12
  • Doing the above you'll need to assign an id to your layout. Add this to your .xml file android:id="@+id/layout1" Commented Jun 2, 2013 at 9:23

2 Answers 2

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

answered Jun 2, 2013 at 9:04
Sign up to request clarification or add additional context in comments.

5 Comments

I didn´t declare it :(. Where do I have to declare it and how do I do it ?
in the same Layout you pass as parameter to setContentView
@blackbelt : Don't advise people to import their own package's R.java. It is done implicitly.
@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 : 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.
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.

answered Jun 2, 2013 at 9:31

5 Comments

where would you add it? I am really new to this :S
in your layout file. that u use in setcontentview(layoutfile.xml)
I addded it in the activity_main.xml file , and it says, <LinearLayout : no orientation specified What did I do wrong
use android:orientation="vertical"
it still says that no orientation is specified :/

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.