如何解决活动的onCreate方法内setContentView行上的错误?

时间:2019-04-25 14:23:45

标签: android

我有一个启动画面活动,并且代码没有错误,我已经通过在其他项目中运行该代码进行了测试。 在我的项目中,相同的代码在setContentView(R.layout.activity_splash_screen)上给出了错误;在onCreate方法中。 我正在尝试在我的启动屏幕中设置一个简短的视频作为背景,并设置一个徽标。

我尝试了通常的解决方案,例如清理和重建,重新检查xml错误等。它仍未在我的应用程序项目中运行,而在其他项目文件上没有任何错误地运行。

这是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

这是SplashScreen.java代码

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;
    }

}

2 个答案:

答案 0 :(得分:0)

您的问题在 activity_splash_screen.xml 文件中。请检查 logo2 可绘制对象。

有关更多详细信息:

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}

答案 1 :(得分:0)

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