MainActivity在Splash Screen LifeCycle之间摧毁自己

时间:2016-02-09 12:58:11

标签: java android splash-screen

我有一个SplashScreen和一个MainActivity,当应用程序启动它时会显示启动画面(延迟3秒)然后显示MainActivity,但当我点击mi App的BarNotification(应用程序外)时,启动画面显示(3)秒延迟)和应用程序崩溃,在LogCat中,MainActivity在Splash Screen Intent LifeCycle之间销毁自己。 (在第29,30行logcat

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


    <WebView
            android:id="@+id/browser"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    />


</LinearLayout>

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:background="@color/white"
        >

    <ImageView android:layout_width="wrap_content"
            android:contentDescription="splash screen"
            android:id="@+id/splash"
            android:src="@drawable/splash"
            android:layout_height="wrap_content"
            android:scaleType="centerInside"/>
</LinearLayout>

为什么我无法通过Bar Notificaction正确启动应用程序?

以下是一些代码:

MainActivity.java

public class MainActivity extends Activity {

    private WebView webview;

    public MainActivity() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        log.debug("onCreate(): " + savedInstanceState);
        MyApplication.startSomeMobileCore(this);
        MyApplication.startSomeMobileNotifier(this);

        setContentView(R.layout.main);
        onNewIntent(getIntent());
    }

    @Override
    protected void onStart() {
        log.debug("onStart()");
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        this.wasRestarted = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    protected void onPause() {
        super.onPause();
        this.receivedIntent = false;
    }

    protected void onStop() {
        super.onStop();
        this.receivedIntent = false;
    }

    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onNewIntent(Intent intent) {
        log.debug("onNewIntent(): " + intent);
        super.onNewIntent(intent);

        if(intent == null) {
            log.warn("Received null intent, will ignore");
        }

        if ("OK".equals(authCode)) {


            if (intent != null && intent.getData() != null &&
                ("content".equals(intent.getData().getScheme()) || 
                "http".equals(intent.getData().getScheme()))) {
                log.debug("intent.getData() :" + intent.getData() + "; intent.getData().getScheme() : " + intent.getData().getScheme());
                String requestedPath;
                if ("http".equals(intent.getData().getScheme())) {
                    requestedPath = URLDecoder.decode(intent.getData().toString());
                } else {
                    requestedPath = intent.getData().getPath();
                }
                showResource(requestedPath);
            } else {
                log.debug("Intent without data -> go to entry page after splash screen");
         showResource(Configuration.properties.getProperty(""));
            }
        } else {
            Intent errorIntent = new Intent(this, ErrorIntent.class);
            startActivity(errorIntent);
            // finish actual activity
            finish();
        }

        log.debug("Show splash screen");
        Intent intentSplash = new Intent(this, SplashIntent.class);
        startActivity(intentSplash);
    }

    void showResource(String resourceToShow) {
        webview = (WebView)findViewById(R.id.browser);
        webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.loadUrl(resourceToShow);
    }
}

SplashIntent.java

public class SplashIntent extends Activity {
    // Time splash screen should be shown (in ms)
    private static final int splashTime = 3000;
    static Logger log = Logger.getLogger(SplashIntent.class);

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        log.debug("SplashIntent: onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
             public void run() {
                 log.debug("SplashIntent: killing splash");
                 finish();
             }
        }, splashTime);

    }
}

logcat

希望你们能帮我解决这个问题......此时我真的没有想法

1 个答案:

答案 0 :(得分:0)

SplashIntent班级

中试试
Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
            @Override
            public void run() {       
                Intent mainIntent = new Intent(Splash.this,MainActivity.class);
                SplashIntent.this.startActivity(mainIntent);
                SplashIntent.this.finish();
            },splashTime);
相关问题