【问题标题】:Getting Null Pointer Exception while implementing Gif image in SplashScreen [duplicate]在 SplashScreen 中实现 Gif 图像时出现空指针异常 [重复]
【发布时间】:2016-06-19 01:09:25
【问题描述】:

我尝试在 SplashScreen 中使用 Gif 图像。当我运行它时,我收到以下错误并且应用程序崩溃了。

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.res.Resources android.content.Context.getResources()”

请帮我解决它。更感谢您的回答。

这是我的代码:

public class SplashScreen extends AppCompatActivity {
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MYGIFView(context));
        context=this;
    }

    public class MYGIFView extends View {
        Movie movie, movie1;
        InputStream is = null, is1 = null;
        long moviestart;

        public MYGIFView(Context con) {
            super(con);           // ERROR IN THIS LINE
            con=context;
            is = con.getResources().openRawResource(R.raw.splash_screen);
            movie = Movie.decodeStream(is);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);
            super.onDraw(canvas);
            long now = android.os.SystemClock.uptimeMillis();
            System.out.println("now=" + now);
            if (moviestart == 0) { // first time
                moviestart = now;
            }
            System.out.println("\tmoviestart=" + moviestart);
            int relTime = (int) ((now - moviestart) % movie.duration());
            System.out.println("time=" + relTime + "\treltime=" + movie.duration());
            movie.setTime(relTime);
            movie.draw(canvas, this.getWidth() / 2 - 20, this.getHeight() / 2 - 40);
            this.invalidate();
        }
    }
}

【问题讨论】:

  • 这两条语句的切换顺序。 1.上下文=这个; 2.setContentView(new MYGIFView(context));
  • 你试过了。但应用程序仍然崩溃。
  • 从 logcat 中获取行号。开始调试。查找未分配的对象

标签: android image gif splash-screen


【解决方案1】:

不要

setContentView(new MYGIFView(context));
context=this;

setContentView(new MYGIFView(SplashScreen.this));

编辑

java.lang.NullPointerException:尝试调用虚拟方法 'android.content.res.Resources android.content.Context.getResources()' 在空对象引用上

context is not passing

NullPointerException 在应用程序尝试使用具有 null 值的对象引用时引发。

你可以试试

 context=this;
 setContentView(new MYGIFView(context));

缺失

setContentView(R.layout.Your_Activity_xml); // Missing
setContentView(new MYGIFView(this));

试试这个

public class SplashScreen extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MYGIFView(SplashScreen.this));

}

【讨论】:

  • 仍然遇到同样的错误@IntelliJ Amiya
  • E/ActivityManager:缩略图尺寸无效:384x384 E/lowmemorykiller:打开 /proc/17463/oom_score_adj 时出错; errno=2 A/libc: 致命信号 11 (SIGSEGV), 代码 1, tid 17493 (lobe.restaurant) 中的故障地址 0x0 E/JavaBinder: !!! BINDER 交易失败!!!
  • 当我再次运行时,logcat 显示了这个。
  • 我的 Gif 图片尺寸是 1080 X 1920
  • 好的。谢谢。让我试着调整它的大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多