[英]Android App crashes after splash screen
我能夠加載啟動畫面,但是3秒鍾后應用程序崩潰了。我想在啟動畫面后加載Main Activity,而且我發現前面在堆棧溢出中討論了相同的主題,但並沒有幫助我。
請幫助我解決問題。
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
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
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.
事件日志
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);
您日志中的錯誤是由以下原因引起的:
IllegalStateException:您需要在此活動中使用Theme.AppCompat主題(或后代)。
我猜想您在AndroidManifest.xml中指定的主題存在問題。 嘗試將其更改為:
機器人:主題= “@安卓風格/ Theme.Holo”
這是在設備上運行時的錯誤消息
此活動已具有窗口裝飾提供的操作欄。 請勿請求Window.FEATURE_SUPPORT_ACTION_BAR並將主題中的windowActionBar設置為false來使用工具欄。
從上面的消息中可以清楚地看出問題。
我通過從MainActivity.java
刪除以下代碼來解決此問題。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
希望此答案對初學者選擇空白模板有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.