I am getting the Not an Enclosing Class
This is my code for ActivityHome.java
.
I am getting the error called ‘Not An Enclosing Class, ActivityLogin’. I have tried many things and I have searched on the web also but I am not getting anything.
The ActivityHome.java
consists of an image and a progress bar. After the progress bar is done, it will take you to the ActivityLogin.java
page which consists of an image and some text.
The problem is that after the ActivityHome.java
page, it is closing the application or repeating that page in a never-ending loop.
package com.splashscreen; import android.animation.ObjectAnimator; import android.content.Intent; import android.os.Handler; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ProgressBar; import com.splashscreen.R; public class ActivityHome extends AppCompatActivity { ProgressBar splashProgress; int SPLASH_TIME = 3000; //This is 3 seconds @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); //This is additional feature, used to run a progress bar splashProgress = findViewById(R.id.splashProgress); playProgress(); //Code to start timer and take action after the timer ends new Handler().postDelayed(new Runnable() { @Override public void run() { //Do any action here. Now we are moving to next page Intent mySuperIntent; mySuperIntent = new Intent(ActivityLogin.this, ActivityHome.class); startActivity(mySuperIntent); //This 'finish()' is for exiting the app when back button pressed from Home page which is ActivityHome finish(); } }, SPLASH_TIME); } //Method to run progress bar for 5 seconds private void playProgress() { ObjectAnimator.ofInt(splashProgress, "progress", 100) .setDuration(5000) .start(); } }
And, this is my code for ActivityLogin.java
.
package com.splashscreen; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.splashscreen.R; public class ActivityLogin extends AppCompatActivity { Button bt_click_me; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); //Add all your codes here bt_click_me = findViewById(R.id.bt_click_me); bt_click_me.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ActivityLogin.this, "You clicked me. Thank you!", Toast.LENGTH_SHORT).show(); } }); } }
I don’t know what the problem is but I am getting a error ‘not an enclosing class: ActivityLogin
;’.
The arguments to your Intent are backwards in a way.
The first parameter is the context, which refers to the current activity, so that should be "Home", and the second parameter is the class of the activity you want to start, so that should be "Login". So it should look like this:
mySuperIntent = new Intent(ActivityHome.this, ActivityLogin.class);
The reason why you get the error is: ActivityLogin.this
means "the instance of ActivityLogin
that is enclosing the current block of code". But this code is not within the ActivityLogin class, so there is no enclosing instance.
Check whether this works
new Handler().postDelayed(new Runnable() { @Override public void run() { Intent mySuperIntent; mySuperIntent = new Intent(ActivityHome.this, ActivityLogin.class); startActivity(mySuperIntent); //You might be closing the newly opened activity previously ActivityHome.this.finish(); } }, SPLASH_TIME);
Hope this helps. Feel free to ask for clarifications…