i was trying some things in android studio and i notice that it doesn't run on android 5.0 or 6.0, it only runs on 7.0. I'm using compileSdkVersion 26, minSdkVersion 21 and targetSdkVersion 26 so you shouldn't have any problems. I really don't understand the logcat error. Any help helps.
Thanks.
This is the code:
public class Juego extends SurfaceView {
Personaje personaje;
Paint paint;
Bitmap imagen_personaje;
public Juego(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
imagen_personaje = BitmapFactory.decodeResource(getResources(), R.drawable.personaje);
personaje = new Personaje(this, imagen_personaje);
}
public void onDraw(Canvas canvas){
paint.setColor(Color.BLACK);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
personaje.onDraw(canvas);
invalidate();
}
}
Character:
public class Personaje {
private Juego juego;
private Bitmap personaje;
Paint paint;
int x, y, inicio = 0, ancho, alto, currentFrame = 0,Horizontal = 1, Vertical = 8, frameLengthInMilliseconds = 150, cantFrames = 8;
long fps, timeThisFrame, lastFrameChangeTime = 0;
public Personaje(Juego juego, Bitmap personaje){
this.juego = juego;
this.personaje = personaje;
this.ancho = personaje.getWidth() / Vertical;
this.alto = personaje.getHeight() / Horizontal;
paint = new Paint();
}
private void update() {
long startFrame = System.currentTimeMillis();
if (startFrame > lastFrameChangeTime + frameLengthInMilliseconds) {
lastFrameChangeTime = startFrame;
currentFrame++;
if (currentFrame >= cantFrames) {
currentFrame = 0;
}
}
long startFrameTime = System.currentTimeMillis();
timeThisFrame = System.currentTimeMillis() - startFrameTime;
if (timeThisFrame >= 1) {
fps = 1000 / timeThisFrame;
}
}
public void onDraw(Canvas canvas) {
update();
int srcX = currentFrame * ancho;
int srcY = alto * inicio;
x = canvas.getWidth()/2;
y = canvas.getHeight()/2;
Rect src = new Rect(srcX, srcY, srcX + ancho, srcY + alto);
Rect dst = new Rect(x, y, x + ancho, y + alto);
canvas.drawBitmap(personaje, src, dst, null);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.agusv.pruebaespacio3.MainActivity">
<com.example.agusv.pruebaespacio3.Juego
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"/>
</android.support.constraint.ConstraintLayout>
Logcat:
04-14 16:43:14.974 3752-3752/com.example.agusv.pruebaespacio3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.agusv.pruebaespacio3, PID: 3752
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.agusv.pruebaespacio3/com.example.agusv.pruebaespacio3.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.example.agusv.pruebaespacio3.Juego
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.example.agusv.pruebaespacio3.Juego
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.agusv.pruebaespacio3.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.example.agusv.pruebaespacio3.Juego
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.agusv.pruebaespacio3.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.agusv.pruebaespacio3.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at com.example.agusv.pruebaespacio3.Personaje.<init>(Personaje.java:28)
at com.example.agusv.pruebaespacio3.Juego.<init>(Juego.java:27)
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.agusv.pruebaespacio3.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
The Bitmap you are passing to the class
Personaje
is referencingnull
. Thus, calling Bitmap methods with the reference tonull
causes you:The problem can occur due to 2 situations.
You do:
Instead of:
Drawable xml
and convert it to Bitmap, so it will returnnull
. For this case the solution is the following:for versions
Android < API 22
Other alternatives: Android getResources().getDrawable() deprecated API 22
Once the Drawable is obtained, try to convert it to Bitmap:
My recommendations:
If you are going to create many objects of type
Juego
, use Glide to avoid the OutOfMemory and have better performance when loading the images. With this you will save on creating the algorithm to recycle theBitmaps
.Always validate possible cases of
null
unless you are 100% sure that your value will always be referenced correctly.I hope I can help you. Greetings.
do you need to use ConstraintLayoyut? I never use it because it gives me problems, because the characteristic of this layout has to be defined, which is that it expands or collapses. I would use a RelativeLayout in your place, you just have to change the XML.
I don't know how to tell you more, the ConstraintLayout always gives me problems.
Greetings, I hope it works for you