2016-06-23 15 views
1

Aufruf der MainActivity.java nach SplashScreen?aufrufen MainActivity nach Splashscreen

Der Code kompiliert korrekt, aber ruft den Main nach dem Splash in keiner Weise auf, es schließt einfach.

In AndroidManifest.xml Aufruf SplashScreen und nicht MainActivity, einen Unterschied machen?

Manifest:

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

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

     <activity 
      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"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

</manifest> 

Splashscreen:

package com.example.stark.hello_world; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.PixelFormat; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

/** 
* Created by Stark on 22/06/2016. 
*/ 
public class Splashscreen extends Activity { 

    public void onAttachedToWindow() { 
     super.onAttachedToWindow(); 
     Window window = getWindow(); 
     window.setFormat(PixelFormat.RGBA_8888); 
    } 
    /** Called when the activity is first created. */ 
    Thread splashTread; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_splashscreen); 
     StartAnimations(); 
    } 
    private void StartAnimations() { 
     Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha); 
     anim.reset(); 
     LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay); 
     l.clearAnimation(); 
     l.startAnimation(anim); 

     anim = AnimationUtils.loadAnimation(this, R.anim.translate); 
     anim.reset(); 
     ImageView iv = (ImageView) findViewById(R.id.splash); 
     iv.clearAnimation(); 
     iv.startAnimation(anim); 

     splashTread = new Thread() { 
      @Override 
      public void run() { 
       try { 
        int waited = 0; 
        // Splash screen pause time 
        while (waited < 3500) { 
         sleep(100); 
         waited += 100; 
        } 
        Intent intent = new Intent(Splashscreen.this,MainActivity.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
        Splashscreen.this.finish(); 

       } catch (InterruptedException e) { 
        // do nothing 
       } finally { 
        Splashscreen.this.finish(); 
       } 

      } 
     }; 
     splashTread.start(); 

    } 

} 

Main:

package com.example.stark.hello_world; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 


public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, 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); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

Antwort

3

Sie beenden den Splashscreen, starten MainActivity jedoch nicht. Füge startActivity (intent) so hinzu;

Intent intent = new Intent(Splashscreen.this,MainActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
startActivity(intent); // Add this 
Splashscreen.this.finish(); 
1

Sie haben nicht erklärt MainActivit y in der AndroidManifest-Datei. Sobald Sie es dort hinzufügen und es funktioniert.

+0

Es andere Tätigkeit beinhaltet und noch Begrüßungsbildschirm, nachdem die Anwendung geschlossen ist :(Ich werde den Code hier –

+1

In Ihrem Splashscreen aktualisieren, haben Sie nicht startActivity Methode aufgerufen. Können Sie das überprüfen. Auch Splashscreen.this .finish(); Diese Zeile wird sowohl in try als auch in final geschrieben, wenn sie in try aufgerufen wird, wird sie schließlich in Exception geworfen. Sie können die Fixes machen. Wenn es immer noch nicht funktioniert, dann posten Sie die logcat-Antwort. Das wird eine bessere Vorstellung von der Ursache geben. – Ani