的setContentView();在我的启动画面活动中出错

时间:2016-09-10 04:08:20

标签: android android-fragments android-activity splash-screen

Hello Android Developers,

我正在创建一个运行欢迎屏幕的应用程序(作为用户安装应用程序的应用程序功能只有一次)然后启动屏幕,其中包含我的应用程序的徽标然后它继续到应用程序我的api级别是最小17(果冻豆4.1)我的应用在 android ver上运行完全稳定。 5.0 + 没有任何错误没有任何问题

但是当我尝试运行我的应用程序Android版本,如 4.1 甚至 4.4.2 时,它会给我错误  的的setContentView(activity_splash_screen); 在欢迎屏幕打开之后崩溃我已经尝试了openign loginscreen欢迎后作为下一个意图而不是splashscreen然后它也给出了与 setContentView(activity_login_screen)相同的错误; 然后我尝试了如果直接让我的mainactivivty在welcomeactivity之后打开(跳过启动和登录)然后它打开主页但没有导航抽屉,没有工具栏名称只打开第一页

我的应用程序原始序列 在ver上运行5.0+

welcomeactivity - > splashscreen - > login-screen-> mainactivity - >完整应用

如果我打开启动画面活动

,欢迎之后启动画面的

错误

09-11 00:20:54.072 1798-1798/com.example.pk.testing_app2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.pk.testing_app2, PID: 1798
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pk.testing_app2/com.example.pk.testing_app2.SplashScreenActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
                                                                              at android.app.ActivityThread.access$900(ActivityThread.java:166)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:146)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5511)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:515)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                              at dalvik.system.NativeStart.main(Native Method)
                                                                           Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>
                                                                              at android.view.LayoutInflater.createView(LayoutInflater.java:626)
                                                                              at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                              at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                                                                              at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366)
                                                                              at android.app.Activity.setContentView(Activity.java:2009)
                                                                              at com.example.pk.testing_app2.SplashScreenActivity.onCreate(SplashScreenActivity.java:14)
                                                                              at android.app.Activity.performCreate(Activity.java:5322)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                              at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:146) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5511) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 
                                                                           Caused by: java.lang.reflect.InvocationTargetException
                                                                              at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                              at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                                                                              at android.view.LayoutInflater.createView(LayoutInflater.java:600)
                                                                              at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                              at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675) 
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700) 
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                                                                              at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366) 
                                                                              at android.app.Activity.setContentView(Activity.java:2009) 
                                                                              at com.example.pk.testing_app2.SplashScreenActivity.onCreate(SplashScreenActivity.java:14) 
                                                                              at android.app.Activity.performCreate(Activity.java:5322) 
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) 
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                              at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:146) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5511) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 
                                                                           Caused by: android.content.res.Resources$NotFoundException: File res/drawable/logo.xml from drawable resource ID #0x7f020060
                                                                              at android.content.res.Resources.loadDrawable(Resources.java:3443)
                                                                              at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                                              at android.widget.ImageView.<init>(ImageView.java:133)
                                                                              at android.widget.ImageView.<init>(ImageView.java:123)
                                                                              at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                              at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
                                                                              at android.view.LayoutInflater.createView(LayoutInflater.java:600) 
                                                                              at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                              at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675) 
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700) 
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                                                                              at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366) 
                                                                              at android.app.Activity.setContentView(Activity.java:2009) 
                                                                              at com.example.pk.testing_app2.SplashScreenActivity.onCreate(SplashScreenActivity.java:14) 
                                                                              at android.app.Activity.performCreate(Activity.java:5322) 
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) 
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                              at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:146) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5511) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 
                                                                           Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
                                                                              at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:986)
                                                                              at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
                                                                              at android.content.res.Resources.loadDrawable(Resources.java:3439)
                                                                              at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 
                                                                              at android.widget.ImageView.<init>(ImageView.java:133) 
                                                                              at android.widget.ImageView.<init>(ImageView.java:123) 
                                                                              at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                              at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
                                                                              at android.view.LayoutInflater.createView(LayoutInflater.java:600) 
                                                                              at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                              at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675) 
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700) 
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                                                                              at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366) 
                                                                              at android.app.Activity.setContentView(Activity.java:2009) 
                                                                              at com.example.pk.testing_app2.SplashScreenActivity.onCreate(SplashScreenActivity.java:14) 
                                                                              at android.app.Activity.performCreate(Activity.java:5322) 
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) 
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                              at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:146) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5511) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 

启动屏幕活动

  import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;


public class SplashScreenActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);



        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashScreenActivity.this, LoginActivity.class));
            }
        }, 1500);
    }

}

1 个答案:

答案 0 :(得分:2)

初始化工具栏后,您正在调用setContentView(R.layout.activity_welcome);。这可能会给你nullPointerExcaption

所以最佳做法是在setContentView(R.layout.activity_welcome);之后写下super.onCreate(savedInstanceState);

示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_welcome);

    // rest of the code..
}