简体   繁体   English

闪屏后Android App崩溃

[英]Android App crashes after splash screen

I am able to load the splash screen but after 3 seconds the app crashes.I want to load the Main Activity after the splash screen.Also I found that the same topic was discussed earlier in stack over flow but it didn't helped me. 我能够加载启动画面,但是3秒钟后应用程序崩溃了。我想在启动画面后加载Main Activity,而且我发现前面在堆栈溢出中讨论了相同的主题,但并没有帮助我。

Please help me solve the issues. 请帮助我解决问题。

AndroidManifest.xml AndroidManifest.xml中

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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" >
        <intent-filter>
            <action android:name="com.example.a2.MAINACTIVITY" />

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

</manifest>

SplashScreen.java SplashScreen.java

package com.example.a2.attemp2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

//        //Remove title bar
//          this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//
//        //Remove notification bar
//       // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//
//        //set content view AFTER ABOVE sequence (to avoid crash)
//        this.setContentView(R.layout.splash);


        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(i);

                    finish();
                }
            }
        };
        timerThread.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

MainActivity.java MainActivity.java

package com.example.a2.attemp2;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @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();
            }
        });
    }

    @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_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);
    }
}

I had recently started another project with same code with different package com.example.jarvis.digin I assure you that there is no compilation errors.

EVENT LOG 事件日志

    10-17 21:46:20.887 7359-7359/com.example.jarvis.digin E/Trace: error opening trace file: No such file or directory (2)
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin W/dalvikvm: VFY: unable to resolve interface method 17898: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin W/dalvikvm: VFY: unable to resolve interface method 17902: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
10-17 21:46:21.357 7359-7359/com.example.jarvis.digin D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin D/AndroidRuntime: Shutting down VM
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419a9300)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime: FATAL EXCEPTION: main
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jarvis.digin/com.example.jarvis.digin.SplashScreen}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:311)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:280)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:254)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.example.jarvis.digin.SplashScreen.onCreate(SplashScreen.java:17)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5008)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
 new android.os.Handler().postDelayed(new Runnable() { 

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
           startActivity(i);


        finish();
        }
    }, 3000);

The error in your log was saying it was caused by: 您日志中的错误是由以下原因引起的:

IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。

I'd guess there is a problem with the theme you have specified in your AndroidManifest.xml. 我猜想您在AndroidManifest.xml中指定的主题存在问题。 Try changing it to something like: 尝试将其更改为:

android:theme="@android:style/Theme.Holo" 机器人:主题= “@安卓风格/ Theme.Holo”

Check out How to change app default theme to a different app theme? 查看如何将应用默认主题更改为其他应用主题?

This is the error message when running on the device 这是在设备上运行时的错误消息

This Activity already has an action bar supplied by the window decor. 此活动已具有窗口装饰提供的操作栏。 Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 请勿请求Window.FEATURE_SUPPORT_ACTION_BAR并将主题中的windowActionBar设置为false来使用工具栏。

The problem is clear from the message above. 从上面的消息中可以清楚地看出问题。

I solved this by removing the following code from MainActivity.java . 我通过从MainActivity.java删除以下代码来解决此问题。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Hope this answer will help beginners when they choose blank template. 希望此答案对初学者选择空白模板有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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