0

So im trying to create a login page for my app and i'm implementing it on Android Studio but it receives an error saying cant not resolve symbol in setContentView. I have been cracking my head up for several hours but i still cant figure what wrong with it. setContentView(R.layout.activity_main); right here at this line, it says activity_main cant not resolve as a variable even though i name my xml file is exactly the same with it. here is my code,

package com.example.tieulyphidep.treasurehunters;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.R;

public class login extends ActionBarActivity implements View.OnClickListener{


    Button bLoggin,buttonSignup;
    EditText EditTextUserName, EditTextPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bLoggin = (Button) findViewById(R.id.buttonLogin);
        buttonSignup = (Button) findViewById(R.id.buttonSignup);
        EditTextUserName =(EditText) findViewById(R.id.EditTextUserName);
        EditTextPassword =(EditText) findViewById(R.id.EditTextPassword);

        buttonLogin.setOnClickListener(this);
        buttonSignup.setOnClickListener(this);


    }
    @Override
    public void onClick(View v) {
    switch(v.getId()){
        case R.id.buttonLogin:
            break;
        case R.id.buttonSignup:
            startActivity(new Intent(this, register.class));
            break;
    }
    }


}
DigitalMan
  • 41
  • 1
  • 7

2 Answers2

3

You imported R as import android.R;, but you want to import com.example.tieulyphidep.R

Just erase the line of import android.R; and Android Studio will suggest you to import the good one.

Note also that a class should start by an upcase letter and end by the mother class, like LoginActivity.

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • if i take import android.R; out it says that R cant be solve as a variable. – DigitalMan Nov 14 '15 at 23:00
  • you don't want to import android.R ! You want to import your own R, from your own package. You erase that line, and Android Studio will note an arror (red subline). If you have the last version af Android Studio, just hit alt+enter when asked, or when targetting the error, and select "import class" – Dan Chaltiel Nov 14 '15 at 23:12
  • so i delete the line import android.R then replace it with import com.example.tieulyphidep.R. then it says it still cant recognize this R. – DigitalMan Nov 14 '15 at 23:18
  • @DigitalMan Build your project. It will generate your project's `R` class. Afterward, you shouldn't need the fully qualified class name, just `R`. – Mike M. Nov 14 '15 at 23:20
  • How are your packages organized ? I wrote com.example.tieulyphidep.R as an example based on your package declaration, it could be `com.example.tieulyphidep.treasurehunters.R` or even something else – Dan Chaltiel Nov 14 '15 at 23:22
0

Do you have a layout file in your layout.xml titled activity_main? This piece of code:

setContentView(R.layout.activity_main);

is basically saying, "Hey, i'm a java class. I want to look like this file in my layout directory: activity_main"

Now, if you don't have an activity_main, you will definitely get an error.

If you already have the activity_main file in your layout, there are a few steps I suggest you take:

  • Clean your project: Build->Clean Project
  • Rebuild your project Build->Rebuild project
  • Restart android studio

I also noted this:

import android.R;

You shouldn't be importing that.

Here's what your code should look like:

package com.USERNAME.loginregister;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Login extends ActionBarActivity implements View.OnClickListener {
    Button bLogin;
    TextView registerLink;
    EditText etUsername, etPassword;

    UserLocalStore userLocalStore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        bLogin = (Button) findViewById(R.id.bLogin);
        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        registerLink = (TextView) findViewById(R.id.tvRegisterLink);

        bLogin.setOnClickListener(this);
        registerLink.setOnClickListener(this);

        userLocalStore = new UserLocalStore(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bLogin:
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();

                User user = new User(username, password);

                authenticate(user);
                break;
            case R.id.tvRegisterLink:
                Intent registerIntent = new Intent(Login.this, Register.class);
                startActivity(registerIntent);
                break;
        }
    }

According to the link you gave me, here is the full project code:

https://github.com/tonikami/LoginRegister

Best of luck,

{Ruchir}
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • I have done all what you said but its still the same. I have activity_main in my layout folder. Also, if i take import android.R; out, it says R cant not be solve as a variable every time i try to revoke it. here is the youtube video im trying to follow. https://www.youtube.com/watch?v=x0I5vJfaRIU. his works but mine doesnt – DigitalMan Nov 14 '15 at 23:09
  • Yes, that is a very common problem. Take it out and then clean and rebuild. If it still doesn't work, [try this.](http://stackoverflow.com/questions/17054000/cannot-resolve-symbol-r-in-android-studio) – Ruchir Baronia Nov 14 '15 at 23:11