Activity does not change

Asked

Viewed 240 times

1

I’m a beginner in Android and am creating an app that has two activities (SplashScreen and ActivityMain).

I managed to bring out the SplashScreen after much sacrifice, however, when installed on the device, appears the message:

****** has stopped

Can someone help me? Below follows the code of SplashScreen

package bbacpropaganda.microfapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Handler;
import android.view.WindowManager;

public class SplashScreen extends AppCompatActivity {
    //Set waktu lama splashscreen
    private static int splashInterval = 2000;

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


//Após alguns segundos vai para atividade principal
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        new Handler().postDelayed(new Runnable() {


            @Override
            public void run() {
                // TODO Auto-generated method stub
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
                //jeda selesai Splashscreen
            }

        }, splashInterval);

//Fim de alguns segundos vai para atividade principal



   }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
  • 1

    can you put the error that appears in the console?? Probably the error happens because you have not declared this new Activity in the manifest.

  • Could be some problem in onCreate of the other Activity also.

  • Lucas and Pablo, fighting for strength!

1 answer

0


I’ve been through the same problem before, so I decided to help.

First change your Androidmanifest.xml File, as in the example below:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:screenOrientation="portrait"
            android:name=".SplashScreen"
            android:label="@string/app_name" >

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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
    </application>
</manifest>

Note that above, I am using a Theme of mine, so if you copy the above code change to your theme.

Also note that in the code above I am using the order of precedence, where the Activity .SplashScreen comes first and takes about 3 seconds to exit the screen, it starts in mode Portrait and then goes on to MainActivity.

Then change your Splashscreen.java file, as done below:

package br.com.novoapp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;


public class SplashScreen extends Activity{

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;
    private Typeface type;

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

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, SPLASH_TIME_OUT);
    }

}

Now create a new file called activity_splash.xml or modify it to display this splash screen, which in this case I used to display a centralized image, which is contained in the folder Drawable, see in the code below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/colorPrimary" >

   <ImageView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/imgLogo"
        android:src="@drawable/telasplash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        tools:ignore="ContentDescription"
        android:layout_gravity="center"
        android:visibility="visible"/>


</RelativeLayout>

Note that in the code above, I put an image center and with a specific color in the background, which you can modify in the file colors.xml, that’s in the folder Values.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.