Reputation: 231
I was following the tutorials online on Android Programming using Eclipse by New Boston and I am stuck at a particular point. So,everything was working fine, I had a button and a TextView in my GUI interface. Now, when i went to my java file and tried to locate it by using the R.Id..... it can't recognize the Button id or TextView Id declared in the XML file. Why am i getting this error? Can someone please correct my mistake. I am using Minimum SDK - API8- Android 2.2 ,Target SDK-API 13-Android 3.2 and Compiling with SDK- API 19- Android 4.4.
I checked the following article on Stack Overflow My Android application cannot find buttons declared in the XML file. As per the solution, I should delete the imports android.R from my java file and i dont have that import at all. So, i am kind of confused.
Main Activity.java
package com.example.androidprogram1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView display;
Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button)findViewById(R.id.button1);
display = (TextView)findViewById(R.id.textView1);
//Error message:- Create field Button1 in type id (Suggestions) /create Constant TextView1 in type id.....This is the error i get. It seems that the id of the button and textView is not adding to the R.java file
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tvView"
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="com.example.androidprogram1.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="65dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/hello_world" />
</RelativeLayout>
Thanks a lot.!!
Upvotes: 0
Views: 831
Reputation: 4235
You need to instantiate the TextView
and Button
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.button1);
display = (TextView) findViewById(R.id.textView1);
}
Edit: Your error message seems to be an Eclipse problem, rather than something you've done, unless you went in and made changes to R.java
, which you shouldn't do. Save your project, close it, re-open and re-build it. Also, if you happen to have import android.R
in your class, remove it.
Upvotes: 1