Problems installing an application

1

I was trying some things in android studio and I realize that it does not run on Android 5.0 or 6.0, it only runs on 7.0. I am using compileSdkVersion 26, minSdkVersion 21 and targetSdkVersion 26 so it should not have any problem. The error of the logcat the truth I do not understand it. Any help is useful.

Thank you.

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) 
    
asked by Agustin Val 14.04.2018 в 18:56
source

2 answers

1

The Bitmap that you are passing to class Personaje is referencing null . Therefore, calling the methods of Bitmap with the reference to null causes you:

  

java.lang.NullPointerException: Attempt to invoke virtual method 'int   android.graphics.Bitmap.getWidth () 'on a null object reference

The problem can occur for 2 situations.

  • You are trying to get the resources without the context:
  • You do:

    imagen_personaje = BitmapFactory.decodeResource(getResources(), R.drawable.personaje);
    

    Instead of:

    imagen_personaje = BitmapFactory.decodeResource(context.getResources(), R.drawable.personaje);
    
  • You are trying to get a resource Drawable xml and convert it to Bitmap, so it will return null . For this case the solution is as follows:
  • For Android < API 22 versions

    Drawable drawable = context.getResources().getDrawable(R.drawable.personaje);
    

    Other alternatives: Android getResources () .getDrawable () deprecated API 22

    Once the Drawable is obtained, try to convert it to Bitmap:

    private Bitmap drawableToBitmap (Drawable drawable) {
    
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }
    
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap); 
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    
        return 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 in creating the algorithm to recycle the Bitmaps .

    • 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.

        
    answered by 14.04.2018 / 22:23
    source
    0

    Is it necessary for you to use ConstraintLayoyut? I never use it because it gives me problems, because you have to define the characteristic of this layout that is to expand or contract. I would use a RelativeLayout instead, you just have to change the XML.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    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"/>
    
    </RelativeLayout>
    

    More I do not know how to say, to me the ConstraintLayout always gives me problems.

    Greetings, I hope it serves you

        
    answered by 14.04.2018 в 19:32