首页 > 解决方案 > How to fix the error on setContentView line inside onCreate method of activity?

问题描述

I have a splash screen activity and the code has no error, I have tested it by running the code in other project. In my project the same code is giving an error on setContentView(R.layout.activity_splash_screen); inside the onCreate method. I am trying to set a short video as background and a logo in my splash screen.

I have tried the usual solutions like clean and rebuild, rechecking for xml mistakes etc. Still it's not running in my app project, while running without any error on other project file.

here's the logcat

 2019-04-25 19:24:47.291 14748-14748/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2019-04-25 19:24:47.291 14748-14748/? E/android.os.Debug: failed to load memtrack module: -2
2019-04-25 19:24:47.709 14767-14767/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2019-04-25 19:24:47.709 14767-14767/? E/android.os.Debug: failed to load memtrack module: -2
2019-04-25 19:24:50.212 14779-14779/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2019-04-25 19:24:50.212 14779-14779/? E/android.os.Debug: failed to load memtrack module: -2
2019-04-25 19:24:50.250 14781-14781/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2019-04-25 19:24:50.250 14781-14781/? E/android.os.Debug: failed to load memtrack module: -2
2019-04-25 19:24:50.638 14804-14804/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2019-04-25 19:24:50.639 14804-14804/? E/android.os.Debug: failed to load memtrack module: -2
2019-04-25 19:24:51.554 14815-14815/com.example.hpnotebook.letshome E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hpnotebook.letshome, PID: 14815
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hpnotebook.letshome/com.example.hpnotebook.letshome.activities.SplashScreen}: android.view.InflateException: Binary XML file line #9: Binary XML file line #14: Error inflating class ImageView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #14: Error inflating class ImageView
     Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class ImageView
     Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.hpnotebook.letshome:mipmap/logo2 with resource ID #0x7f0f0003
     Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.hpnotebook.letshome:mipmap/logo2" (7f0f0003) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f0f0003 a=-1 r=0x7f0f0003}
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:687)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
        at android.content.res.Resources.loadDrawable(Resources.java:858)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
        at android.widget.ImageView.<init>(ImageView.java:162)
        at android.widget.ImageView.<init>(ImageView.java:150)
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
        at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
        at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
        at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:994)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.hpnotebook.letshome.activities.SplashScreen.onCreate(SplashScreen.java:19)
        at android.app.Activity.performCreate(Activity.java:6679)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
2019-04-25 19:24:51.554 14815-14815/com.example.hpnotebook.letshome E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
2019-04-25 19:24:52.271 1782-1782/? E/ResourcesManager: failed to add asset path /data/app/com.example.hpnotebook.letshome-2/base.apk
2019-04-25 19:24:52.272 1782-1782/? E/ResourcesManager: failed to add asset path /data/app/com.example.hpnotebook.letshome-2/base.apk
2019-04-25 19:24:52.272 1782-1782/? E/ResourcesManager: failed to add asset path /data/app/com.example.hpnotebook.letshome-2/base.apk
2019-04-25 19:24:52.273 1782-1782/? E/ResourcesManager: failed to add asset path /data/app/com.example.hpnotebook.letshome-2/base.apk

Here's the SplashScreen.java code

package com.example.hpnotebook.letshome.activities;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.VideoView;
import com.example.hpnotebook.letshome.R;

public class SplashScreen extends AppCompatActivity {

    private VideoView videoBG;
    MediaPlayer mMediaPlayer;
    int mCurrentVideoPosition;

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

        videoBG = (VideoView) findViewById(R.id.videoView);
        Uri uri = Uri.parse("android.resource://" // First start with this,
                + getPackageName() // then retrieve your package name,
                + "/" // add a slash,
                + R.raw.snowy_mountains_video); // and then finally add your video resource. Make sure it is stored
        // in the raw folder.

        // Set the new Uri to our VideoView
        videoBG.setVideoURI(uri);
        // Start the VideoView
        videoBG.start();

        // Set an OnPreparedListener for our VideoView. For more information about VideoViews,
        // check out the Android Docs: https://developer.android.com/reference/android/widget/VideoView.html
        videoBG.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mMediaPlayer = mediaPlayer;
                // We want our video to play over and over so we set looping to true.
                mMediaPlayer.setLooping(true);
                // We then seek to the current posistion if it has been set and play the video.
                if (mCurrentVideoPosition != 0) {
                    mMediaPlayer.seekTo(mCurrentVideoPosition);
                    mMediaPlayer.start();
                }
            }
        });
    }

    /*================================ Important Section! ================================
    We must override onPause(), onResume(), and onDestroy() to properly handle our
    VideoView.
     */

    @Override
    protected void onPause() {
        super.onPause();
        // Capture the current video position and pause the video.
        mCurrentVideoPosition = mMediaPlayer.getCurrentPosition();
        videoBG.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Restart the video when resuming the Activity
        videoBG.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // When the Activity is destroyed, release our MediaPlayer and set it to null.
        mMediaPlayer.release();
        mMediaPlayer = null;
    }

}

标签: android

解决方案


请检查您在 activity_splash_screen 中声明的图像视图


推荐阅读