Christos312
Christos312

Reputation: 466

My buttons do not appear when I try to findViewById

I know that there are tons of similar questions because I have read them ALL but my I cannot solve my problem. I have the simpliest setup ever...

XML

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:padding="@dimen/padding_medium"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignRight="@+id/textView1"
    android:layout_marginBottom="60dp"
    android:layout_marginRight="46dp"
    android:text="Button" />

java

import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button my_btn = (Button) findViewById(R.layout.button1);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

Error

button1 cannot be resolved or is not a field    MainActivity.java

I don't know if this is make any difference but I am using the latest API and the Eclipse Version is Juno Any kind of help is much appreciated!!!!!

Upvotes: 0

Views: 283

Answers (1)

Christos312
Christos312

Reputation: 466

Idiot alert....

Button my_btn = (Button) findViewById(R.layout.button1);

This should be

Button my_btn = (Button) findViewById(R.id.button1);

Sorry for wasting your time!

Upvotes: 1

Related Questions