Reputation: 1
I'm am doing a registration screen as a first page in my application, in the MainActivity.java I put :
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView3);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view){
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")){
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(false);
}
}}
@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;
}
}
In the xml page I put :
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="92dp"
android:text="username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="36dp"
android:text="password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="30dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:text="attempts" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="42dp"
android:text="login" />
</RelativeLayout>
The error I get is in the java code when writing :
username = (EditText)findViewById(R.id.editText1);
editText1 cannot be resolved or is not a field. Same to editText2 , textView3 and button1
Please any help ?
Upvotes: 0
Views: 143
Reputation:
I have own solution.I hope it helps to you .
In Project->Clean
I select "Clean projects selected below", select my project(s) and check "Start a build immediately" with "Build only selected projects".
Then go to Window->Preferences->General->Keys
, search "Build clean" and enter my own binding for this command. For example, Ctrl + D
Then, when I press Ctrl + D and Enter I have clean and build.
Upvotes: 0
Reputation: 1358
First Clean and rebuild your project. If no luck then try the following.
Sometimes this is happening in xml layout files when missing a closing tag or something like that. Just double check your xml to make sure that you didn't forget something. Also
check <?xml version="1.0" encoding="utf-8"?>
is in the top of the layout file.
Upvotes: 0
Reputation: 4964
First , save
the file (layout) , and try clean and build the project
Then : change the declaration to :
private EditText username;
private EditText password;
Also change the name of login method
because you have a button
named login
to loginFunction
for example,
public void loginFunction(View view)
ans associate this method to button login
in setOnclickListener
:
login.setOnclickListener(loginFunction)
Upvotes: 0
Reputation: 670
When you assign an ID like @+id/editText1
in your layout file, your IDE should usually take care of generating the values in R.java for you. If that code generation step hasn't happened or has failed for some reason, then R.id.editText1 won't be available and you'll get the error you described.
Which IDE are you using? Can you clean and rebuild the project?
Did you have a look at R.java to see if your field is in there? For each ID in any of the layout files you should see a constant generated in there, like this:
public final class R {
(...)
public static final class id {
public static final int editText1 = ...;
}
(...)
}
Upvotes: 1