Bom dia
Estou com um problema que está indicando que o
android.content.res.Resources android.content.Context.getResources()' on a null object reference
está vazio mas a imagem está no caminho re/drawable/cano nem imagino o que tem de errado no código... desde já agradeço
02-28 13:10:32.139 1112-1112/br.com.alura.jumper I/art: Not late-enabling -Xcheck:jni (already on)
02-28 13:10:32.140 1112-1112/br.com.alura.jumper I/art: Late-enabling JIT
02-28 13:10:32.690 1112-1112/br.com.alura.jumper I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
02-28 13:10:34.238 1112-1112/br.com.alura.jumper W/System: ClassLoader referenced unknown path: /data/app/br.com.alura.jumper-2/lib/arm
02-28 13:10:34.432 1112-1123/br.com.alura.jumper W/art: Suspending all threads took: 10.833ms
02-28 13:10:35.884 1112-1123/br.com.alura.jumper W/art: Suspending all threads took: 23.475ms
02-28 13:10:36.064 1112-1127/br.com.alura.jumper I/art: Background partial concurrent mark sweep GC freed 128(6KB) AllocSpace objects, 0(0B) LOS objects, 30% free, 9MB/13MB, paused 2.808ms total 116.715ms
02-28 13:10:37.014 1112-1159/br.com.alura.jumper D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-28 13:10:37.057 1112-1112/br.com.alura.jumper D/: HostConnection::get() New Host Connection established 0xb4e5e980, tid 1112
02-28 13:10:37.894 1112-1123/br.com.alura.jumper W/art: Suspending all threads took: 43.563ms
02-28 13:10:37.987 1112-1159/br.com.alura.jumper D/: HostConnection::get() New Host Connection established 0xac7ff020, tid 1159
02-28 13:10:37.998 1112-1159/br.com.alura.jumper I/OpenGLRenderer: Initialized EGL, version 1.4
02-28 13:10:38.076 1112-1159/br.com.alura.jumper W/EGL_emulation: eglSurfaceAttrib not implemented
02-28 13:10:38.076 1112-1159/br.com.alura.jumper W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xae9bebe0, error=EGL_SUCCESS
02-28 13:10:38.270 1112-1156/br.com.alura.jumper D/: HostConnection::get() New Host Connection established 0xb4df2150, tid 1156
02-28 13:10:48.879 1112-1123/br.com.alura.jumper W/art: Suspending all threads took: 6.872ms
02-28 13:11:08.506 1112-1156/br.com.alura.jumper E/AndroidRuntime: FATAL EXCEPTION: Thread-99
02-28 13:11:08.506 1112-1156/br.com.alura.jumper E/AndroidRuntime: Process: br.com.alura.jumper, PID: 1112
02-28 13:11:08.506 1112-1156/br.com.alura.jumper E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
02-28 13:11:08.506 1112-1156/br.com.alura.jumper E/AndroidRuntime: at br.com.alura.jumper.elements.Cano.<init>(Cano.java:46)
02-28 13:11:08.506 1112-1156/br.com.alura.jumper E/AndroidRuntime: at br.com.alura.jumper.elements.Canos.move(Canos.java:48)
02-28 13:11:08.506 1112-1156/br.com.alura.jumper E/AndroidRuntime: at br.com.alura.jumper.engine.Game.run(Game.java:67)
02-28 13:11:08.506 1112-1156/br.com.alura.jumper E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
02-28 13:14:01.188 1112-1156/br.com.alura.jumper I/Process: Sending signal. PID: 1112 SIG: 9
package br.com.alura.jumper.elements;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import br.com.alura.jumper.R;
import br.com.alura.jumper.graphic.Cores;
import br.com.alura.jumper.graphic.Tela;
public class Cano {
private Tela tela;
private int alturaDoCanoInferior;
private int alturaDoCanoSuperior;
private static final int TAMANHO_DO_CANO = 250;
private static final int LARGURA_DO_CANO = 100;
private static final Paint VERDE = Cores.getCorDoCano();
private int posicao;
private Bitmap canoInferior;
private final Bitmap canoSuperior;
/*
public Cano(Tela tela, int posicao, Context context){
this.tela = tela;
this.posicao = posicao;
alturaDoCanoInferior = tela.getAltura() - TAMANHO_DO_CANO + valorAleatorio();
alturaDoCanoSuperior = 0 + TAMANHO_DO_CANO + valorAleatorio();
Bitmap bp = BitmapFactory.decodeResource(context.getResources(), R.drawable.cano);
this.canoInferior = Bitmap.createScaledBitmap(bp, LARGURA_DO_CANO, this.alturaDoCanoInferior, false);
this.canoSuperior = Bitmap.createScaledBitmap(bp, LARGURA_DO_CANO, alturaDoCanoSuperior, false);
//canoInferior = Bitmap.createScaledBitmap(bp, LARGURA_DO_CANO, alturaDoCanoInferior, false);
//canoSuperior = Bitmap.createScaledBitmap(bp, LARGURA_DO_CANO, alturaDoCanoSuperior, false);
}*/
public Cano(Tela tela, int posicao, Context context) {
this.tela = tela;
this.posicao = posicao;
this.alturaDoCanoSuperior = TAMANHO_DO_CANO + valorAleatorio();
this.alturaDoCanoInferior = tela.getAltura() - TAMANHO_DO_CANO - valorAleatorio();
Bitmap bp = BitmapFactory.decodeResource(context.getResources(), R.drawable.cano);
this.canoInferior = Bitmap.createScaledBitmap(bp, LARGURA_DO_CANO, this.alturaDoCanoInferior, false);
this.canoSuperior = Bitmap.createScaledBitmap(bp, LARGURA_DO_CANO, this.alturaDoCanoSuperior, false);
}
private int valorAleatorio() {
return (int) (Math.random() * 150);
}
public void desenhaNo(Canvas canvas){
desenhaCanoInferiorNo(canvas);
desenhaCanoSuperiorNo(canvas);
}
private void desenhaCanoSuperiorNo(Canvas canvas){
//canvas.drawRect(posicao, 0, posicao + LARGURA_DO_CANO, alturaDoCanoSuperior, VERDE);
canvas.drawBitmap(canoSuperior,posicao, 0, null);
}
private void desenhaCanoInferiorNo(Canvas canvas) {
//canvas.drawRect(posicao, alturaDoCanoInferior, posicao + LARGURA_DO_CANO, tela.getAltura(), VERDE);
canvas.drawBitmap(canoInferior,posicao, alturaDoCanoInferior, null);
}
public void move() {
this.posicao -= 5;
}
public boolean saiuDaTela() {
return posicao + LARGURA_DO_CANO < 0;
}
public int getPosicao() {
return posicao;
}
public boolean temColisaoHorizontalCom(Passaro passaro) {
return this.posicao < passaro.X + passaro.RAIO;
}
public boolean temColisaoVerticalCom(Passaro passaro) {
return passaro.getAltura() - passaro.RAIO < this.alturaDoCanoSuperior
&& passaro.getAltura() + passaro.RAIO > this.alturaDoCanoInferior;
}
}
package br.com.alura.jumper.elements;
import android.content.Context;
import android.graphics.Canvas;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import br.com.alura.jumper.graphic.Tela;
public class Canos {
private final List<Cano> canos = new ArrayList<Cano>();
private static final int QUANTIDADE_CANOS = 5;
private static final int DISTANCIA_ENTRE_CANOS = 200;
private final Pontuacao pontuacao;
private Tela tela;
private Context context;
public Canos(Tela tela, Pontuacao pontuacao, Context context){
this.tela = tela;
this.pontuacao = pontuacao;
int posicao = 400;
for (int i = 0; i < QUANTIDADE_CANOS; i++){
posicao += DISTANCIA_ENTRE_CANOS;
Cano cano = new Cano(tela, posicao, context);
canos.add(cano);
}
}
public void desenhaNo(Canvas canvas) {
for(Cano cano : canos){
cano.desenhaNo(canvas);
}
}
public void move(){
ListIterator<Cano> iterator = canos.listIterator();
while (iterator.hasNext()){
Cano cano = iterator.next();
cano.move();
if (cano.saiuDaTela()) {
pontuacao.aumenta();
iterator.remove();
Cano outroCano = new Cano(tela, getMaximo() + DISTANCIA_ENTRE_CANOS, context);
iterator.add(outroCano);
}
}
}
private int getMaximo() {
int maximo = 0;
for(Cano cano : canos){
maximo = Math.max(cano.getPosicao(), maximo);
}
return maximo;
}
public boolean temColisaoCom(Passaro passaro) {
for (Cano cano : canos){
if(cano.temColisaoHorizontalCom(passaro) && cano.temColisaoVerticalCom(passaro)){
return true;
}
}
return false;
}
}
package br.com.alura.jumper.engine;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import br.com.alura.jumper.R;
import br.com.alura.jumper.elements.Cano;
import br.com.alura.jumper.elements.Canos;
import br.com.alura.jumper.elements.GameOver;
import br.com.alura.jumper.elements.Passaro;
import br.com.alura.jumper.elements.Pontuacao;
import br.com.alura.jumper.graphic.Tela;
public class Game extends SurfaceView implements Runnable, View.OnTouchListener {
private final Context context;
private boolean isRunning = true;
private SurfaceHolder holder = getHolder();
private Passaro passaro;
private Canos canos;
private Canvas canvas;
private Bitmap background;
private Tela tela;
private Pontuacao pontuacao;
private Som som;
public Game(Context context) {
super(context);
this.context = context;
tela = new Tela(context);
//som = new Som(context);
inicializaElementos();
setOnTouchListener(this);
}
private void inicializaElementos(){
passaro = new Passaro(tela,context,som);
pontuacao = new Pontuacao();
canos = new Canos(tela,pontuacao, context);
Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.background);
background = Bitmap.createScaledBitmap(back, back.getWidth(), tela.getAltura(),false);
}
@Override
public void run() {
while (isRunning){
if(!holder.getSurface().isValid()) continue;
canvas = holder.lockCanvas();
canvas.drawBitmap(background, 0, 0, null);
passaro.desenhaNo(canvas);
passaro.cai();
canos.desenhaNo(canvas);
canos.move();
pontuacao.desenhaNo(canvas);
if(new VerificadorDeColisao(passaro, canos).temColisao()){
new GameOver(tela).desenhaNo(canvas);
isRunning = false;
}
holder.unlockCanvasAndPost(canvas);
}
}
public void inicia() {
isRunning = true;
}
public void pausa() {
isRunning = false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
passaro.pula();
return false;
}
}