有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android应用程序在启动屏幕后崩溃

我可以加载启动屏幕,但3秒钟后应用程序崩溃。我想在启动屏幕后加载主要活动。我还发现,前面的stack over flow中也讨论过同样的话题,但这对我没有帮助

请帮我解决这些问题

AndroidManifest.xml

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

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

    <activity
        安卓:name=".SplashScreen"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />

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

    <activity
        安卓:name=".MainActivity"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="com.example.a2.MAINACTIVITY" />

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

</manifest>

SplashScreen.java

package com.example.a2.attemp2;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.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

package com.example.a2.attemp2;
import 安卓.os.Bundle;
import 安卓.support.design.widget.FloatingActionButton;
import 安卓.support.design.widget.Snackbar;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.view.View;
import 安卓.view.Menu;
import 安卓.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 (L安卓/view/SearchEvent;)
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin I/dalvikvm: Could not find method 安卓.view.Window$Callback.onSearchRequested, referenced from method 安卓.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: L安卓/view/Window$Callback;.onSearchRequested (L安卓/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 安卓.view.Window$Callback.onWindowStartingActionMode, referenced from method 安卓.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: L安卓/view/Window$Callback;.onWindowStartingActionMode (L安卓/view/ActionMode$Callback;I)L安卓/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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.app.ActivityThread.access$600(ActivityThread.java:130)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.os.Handler.dispatchMessage(Handler.java:99)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.os.Looper.loop(Looper.java:137)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.安卓.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 安卓.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:311)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:280)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:254)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.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 安卓.app.Activity.performCreate(Activity.java:5008)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.app.ActivityThread.access$600(ActivityThread.java:130) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.os.Handler.dispatchMessage(Handler.java:99) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.os.Looper.loop(Looper.java:137) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.安卓.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) 

共 (2) 个答案

  1. # 1 楼答案

     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);
    
  2. # 2 楼答案

    这是在设备上运行时的错误消息

    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.

    从上面的信息中可以清楚地看出问题所在

    我通过从MainActivity.java中删除以下代码解决了这个问题

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

    希望这个答案能帮助初学者选择空白模板