简体   繁体   中英

Cannot get android studio app to start login screen after initial splash screen

I am new to java coding and Android Studio, so please bear with me. However, I am trying to get a login screen to start after the splash screen and the app crashes after the splash screen. The splash screen works no problem. Anyways, here is the first set of code and this is the splash screen code in Main activity.

package com.example.xxxx.safetyxxxxxxx;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView tv;
    ImageView iv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        tv = (TextView) findViewById(R.id.tv);
        iv = (ImageView) findViewById(R.id.imageView);
        Animation mine = AnimationUtils.loadAnimation(this, R.anim.transition);
        final Intent go = new Intent(MainActivity.this, LoginPageActivity.class);
        //set duration ... 1 second ... :p
        mine.setDuration(1000);
        tv.startAnimation(mine);
        iv.startAnimation(mine);
        //make a thread to go to second activity...
        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    MainActivity.this.startActivity(go);
                    finish();
                }
            }
        };
        t.start();
    }


    }

this is the Login Page activity called "LoginPageActivity.java" that I would like to have the app go to after the splash screen.

package com.example.xxxx.safetyxxxxxxx;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import static com.example.xxxx.safetyxxxxxxx.R.layout.activity_login_page;

public class LoginPageActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_login_page);

    findViewById(R.id.textViewSignUp).setOnClickListener(this);

}


    @Override
    public void onClick(View view) {
            switch (view.getId()) {
                case R.id.textViewSignUp:

                    startActivity(new Intent(this, SignUpActivity.class));

                    break;
            }
        }
    }

This is the androidmanifest.xml code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xxxx.safetyxxxxxxx">

    <application
        android:name=".Database"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LoginPageActivity"
            android:label="@string/app_name">
    </activity>
        <activity android:name=".Main2Activity" />
        <activity android:name=".SignUpActivity" />
    </application>

</manifest>

Here is the logcat error

1-28 12:55:33.490 7647-7670/com.example.xxxx.safetyxxxxxxx E/AndroidRuntime: FATAL EXCEPTION: Thread-5
                                                                              Process: com.example.xxxx.safetyxxxxxxx, PID: 7647
                                                                              android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.xxxx.safetyxxxxxxx/com.example.xxxx.safetyxxxxxxx.LoginPageActivity}; have you declared this activity in your AndroidManifest.xml?
                                                                                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1932)
                                                                                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1615)
                                                                                  at android.app.Activity.startActivityForResult(Activity.java:4472)
                                                                                  at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
                                                                                  at android.app.Activity.startActivityForResult(Activity.java:4430)
                                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
                                                                                  at android.app.Activity.startActivity(Activity.java:4791)
                                                                                  at android.app.Activity.startActivity(Activity.java:4759)
                                                                                  at com.example.mike.safetychecker.MainActivity$1.run(MainActivity.java:39)

Also if needed the file activity_login_page.xml code is below

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginPageActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="Hi, Welcome to Safety xxxxxx Please Login or Signup"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.995"
        tools:layout_editor_absoluteX="0dp"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Login"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="398dp"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="346dp"
        android:layout_height="50dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:ems="10"
        android:hint="email"
        android:inputType="textEmailAddress"
        android:text=" email"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="270dp"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/Password"
        android:layout_width="346dp"
        android:layout_height="50dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:ems="10"
        android:inputType="textVisiblePassword"
        android:text=" Password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="336dp"
        tools:ignore="MissingConstraints" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="219dp"
        android:layout_height="229dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/editTextEmail"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/safetyxxxx" />

    <TextView
        android:id="@+id/textViewSignUp"
        android:layout_width="351dp"
        android:layout_height="34dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Do Not Have An Account?  Click Here"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.529"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="459dp"
        tools:ignore="MissingConstraints" />

</android.support.constraint.ConstraintLayout>

Also there is this transition.xml file contained in a anim folder that might help

<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    />

You have to declare your Activity in your AndroidManifest.xml. Almost all of your system-related classes have to be declared in it, this includes Activities, Services, BroadCast Receivers. You can read up on how Manifest works over here

Back to your problem, you can fix it by add the following line inside the application tag in your Android Manifest. Remove any intent filters you have applied to it

<activity android:name=".LoginPageActivity" />

you have to try to open your LoginPageActivity like this way

Paste this code

openActivity();

instead of this

Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    MainActivity.this.startActivity(go);
                    finish();
                }
            }
        };
        t.start();

and put this method

public void openActivity()
    {
        final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent go = new Intent(MainActivity.this,LoginPageActivity.class);
                         startActivity(go);
                        finish();

                    }
                }, 5000);
    }

import this packages

import android.os.Bundle;
import android.os.Handler;

and also mention in your manifest like this

<activity android:name=".LoginPageActivity"
        android:label="@string/app_name"/>

Remove :

<intent-filter>
    <action android:name="com.example.xxxx.safetyxxxxxxx.LoginPageActivity" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Because there is always remains only one LAUNCHER activity in application. And also intent filter is wrong there.

change LAUNCHER TO DEFAULT in your mainfest, Two launcher is not possible at same time (Assuming MainActivity as LAUNCHER)

</activity android:name=".LoginPageActivity>

        <intent-filter>
            <action android:name="com.example.xxxx.safetyxxxxxxx.LoginPageActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

use this

<activity android:name=".LoginPageActivity"
        android:label="@string/app_name"/>

instead of

 <activity android:name=".LoginPageActivity"
        android:label="@string/app_name">
    <intent-filter>
        <action android:name="com.example.xxxx.safetyxxxxxxx.LoginPageActivity" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    Intent intent = new Intent(MainActivity.this,com.example.xxxx.safetyxxxxxxx.LoginPageActivity.class);
                     startActivity(intent);
                    finish();

                }
            }, 5000);

//where 5000 is the delayed time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM