###前提
SurfaceViewを使ってアプリを作っているのですが、一旦androidの端末の右下にあるメニューボタンを押してアプリ一覧に遷移した後再びアプリに戻ろうとすると下記のエラーが出ます。これは半ばソースにも記していますがufoという画像の要素に対してgetWidth()で幅を求めているものです。そこでエラーが出ました。booleanやthreadがnullではないかということを用いてnullの時はそもそも呼び出しがないようにしているつもりなのですがこのようなエラーが出てしまいます。
どのような対策があるのか教えてください、よろしくお願いします。
*新たにエラーが出ました(会話参照)
###発生している問題・エラーメッセージ
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at jpdb.shoichi_vono_kono.airchainsaw.C.run(C.java:155) at java.lang.Thread.run(Thread.java:818)
###該当のソースコード
java
1@Override 2 public void surfaceCreated(SurfaceHolder holder) { 3 isSurfaceStanby = true; 4 thread = new Thread(this); 5 thread.start(); 6 } 7 @Override 8 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 9 10 } 11 @Override 12 public void surfaceDestroyed(SurfaceHolder holder) { 13 isSurfaceStanby = false; 14 thread = null; 15 if (ufo != null) { 16 ufo.recycle(); 17 ufo = null; 18 } 19 if (starmae != null) { 20 starmae.recycle(); 21 starmae = null; 22 } 23 if (starushiro != null) { 24 starushiro.recycle(); 25 starushiro = null; 26 } 27 } 28@Override 29 public void run() { 30 Canvas c; 31 while (thread != null) { 32 if (!isSurfaceStanby) return; 33 c = holder.lockCanvas(); 34 if (c == null) return; 35 c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);//ここでも同じエラーが出ます 36 if (linebool) { 37 lineh += winH / 20; 38 if (lineh >= winH) { 39 linebool = false; 40 } 41 }else{ 42 lineh -= winH / 20; 43 if (lineh <= 0) { 44 linebool = true; 45 } 46 } 47 winW = getWidth(); 48 winH = getHeight(); 49 if (!isSurfaceStanby) return; 50 if (c == null) return; 51 ufow = ufo.getWidth();//エラーの箇所です 52 ufoh = ufo.getHeight(); 53//省略 54 holder.unlockCanvasAndPost(c); 55 try { 56 Thread.sleep(50); 57 }catch (Exception e) { 58 59 } 60 } 61 }
修正記述↓
//ここから//ここまで、の部分が追記です。
java
1@Override 2 public void surfaceCreated(SurfaceHolder holder) { 3//ここから 4 res = this.getContext().getResources(); 5 ufo = BitmapFactory.decodeResource(res, R.drawable.ufocomp); 6 starmae = BitmapFactory.decodeResource(res, R.drawable.starskymae); 7 starushiro = BitmapFactory.decodeResource(res, R.drawable.starskyushiro); 8//ここまで 9 isSurfaceStanby = true; 10 thread = new Thread(this); 11 thread.start(); 12 }
↓
エラー
java.lang.OutOfMemoryError
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/12 07:35
2017/06/12 07:44
2017/06/12 08:26
2017/06/12 08:31
2017/06/12 08:42
2017/06/12 09:10
2017/06/12 09:16
2017/06/12 09:28
2017/06/13 08:56
2017/06/13 09:04 編集
2017/06/13 14:01
2017/06/13 14:05
2017/06/13 14:44 編集
2017/06/14 04:09