设置内容视图();在我的启动画面活动中出错

Posted

技术标签:

【中文标题】设置内容视图();在我的启动画面活动中出错【英文标题】:setContentView(); giving error in my splash screen activity 【发布时间】:2016-09-10 04:08:20 【问题描述】:

android 开发者您好,

我正在制作一个运行欢迎屏幕的应用程序(作为应用程序功能仅在用户安装应用程序时使用一次),然后启动包含我的应用程序徽标的屏幕,然后它继续运行应用程序我的 api 级别是最低 17(果冻豆4.1) 我的应用在 android 版本上运行 full stable。 5.0+没有任何错误没有任何问题


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


我的应用原始序列 在版本上运行。 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 ComponentInfocom.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);
    


【问题讨论】:

我认为在找到工具栏之前你应该做 setcontentview 你检查过你的启动画面的 xml 文件吗?并在这里发布你完整的 logcat。 我现在已经更新了 logcat 的启动画面和欢迎屏幕,现在只有它会在启动画面中崩溃:( 【参考方案1】:

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

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

示例:

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

    setContentView(R.layout.activity_welcome);

    // rest of the code..

【讨论】:

"thanks" 我已经做到了,现在欢迎屏幕正在运行,但是在那个 setcontetview 行 上的 spalsh 屏幕崩溃之后,如果我在欢迎后打开登录活动,那么它也会崩溃同一行 setcontetview of login activity 我对此给出了错误,请查看并告知是否有任何解决方案 错误在您的 xml 文件中。您可以发布您的 SplashScreenActivity xml 文件吗? 我解决了我认为较低版本不支持矢量图像的问题。像下面的 5.0 我使用了 .png 图像,它工作正常谢谢你提请我注意 xml 文件 任何时候..如果对您有帮助,您可以标记此答案。 :)

以上是关于设置内容视图();在我的启动画面活动中出错的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 android 电视应用中添加启动画面?

启动画面应用程序并隐藏操作栏

如何像启动画面一样只运行一次活动

启动画面后没有出现主要活动

添加启动画面活动时“客户端尚未准备好”

iPhone应用程序的横向启动画面?