Field can be converted to a local variable ,convert field to local variable in onCreate method

47 views Asked by At
public class LoginActivity extends AppCompatActivity {

    private TextView Dont_have_any_account_lets_create;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EdgeToEdge.enable(this);

        Dont_have_any_account_lets_create = findViewById(R.id.Dont_have_any_account_lets_create);
        if (Dont_have_any_account_lets_create != null) {
            Dont_have_any_account_lets_create.setOnClickListener(v -> {
                Intent intent = new Intent(LoginActivity.this, SignUp.class);
                startActivity(intent);
            });
        } else {
            Log.e("LoginActivity", "Dont_have_any_account_lets_create TextView is null");
        }

I am new to android developing. This is basically a login page , there is option for those having no account , so when they clicked it opens a signup form, but when i click on that button (for testing), dont have any account, the app crashes. here in java code i am getting this error , i thought may be this error is the main reason for that. Please help me with this. Thanks.

1

There are 1 answers

1
Dan Gerchcovich On BEST ANSWER

First off, please make sure when posting an issue like this, you post a stack trace so others can help you.

To get the stack trace on Android Studio, you can go to the logcat of the device you were running the app on (you don't need to be in debug mode to do this). If you can't do it through Android studio, you can run this on your terminal (assuming you've configured your environment variables properly):

logcat

Then run the app, and get the logs for where the crash happened.

Second in terms of the crash that happened, the crash most likely came from the SignUp activity. Make sure that the Signup Activitiy inherits from the same theme as the LoginActivity. If you're using AppCompatActivity you need to make sure its style inherits from a descendant of Theme.AppCompat (for example Theme.AppCompat.Light.NoActionBar).

Go into your styles.xml and apply this theme on your Activities theme settings.

Third, since you're new to Android development, you should look into Single Activity Architecture, which is the Google Recommended architecture for new devices. It's a concept where the entire app is managed by a Single activity, and users navigate across the app via Fragments & Fragment Transactions. These transactions are managed via the Android Navigation Library:

https://developer.android.com/guide/navigation