Zekirak
Zekirak

Reputation: 191

Problems with the api

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't have any problem. I don't understand the error of the logcat. Someone can help me?

Thanks

This is the code:

public class Juego extends SurfaceView {

Personaje personaje;
Paint paint;

public Juego(Context context, AttributeSet attrs) {
    super(context, attrs);

    paint = new Paint();

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

    Bitmap bitmap = ((BitmapDrawable) image).getBitmap();

    personaje = new Personaje(this, bitmap);

}

public void onDraw(Canvas canvas){

    paint.setColor(Color.BLACK);
    canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

    personaje.onDraw(canvas);
    invalidate();
}

}

Personaje:

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:

 Process: com.example.agusv.pruebaespacio3, PID: 4757
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.agusv.pruebaespacio3/com.example.agusv.pruebaespacio3.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.agusv.pruebaespacio3.Juego
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.agusv.pruebaespacio3.Juego
    at android.view.LayoutInflater.createView(LayoutInflater.java:633)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    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:12)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
    at android.view.LayoutInflater.createView(LayoutInflater.java:607)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    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:12) 
    at android.app.Activity.performCreate(Activity.java:5990) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f060055
    at android.content.res.Resources.getValue(Resources.java:1266)
    at android.content.res.Resources.getDrawable(Resources.java:785)
    at android.content.res.Resources.getDrawable(Resources.java:752)
    at com.example.agusv.pruebaespacio3.Juego.<init>(Juego.java:30)
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    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:12) 
    at android.app.Activity.performCreate(Activity.java:5990) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Upvotes: 2

Views: 44

Answers (1)

Tom Bailey
Tom Bailey

Reputation: 702

Looking at your stack trace, this seems to be the actual issue:

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f060055

Probably caused by:

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

You should be able to check this my opening "R.java" and looking for the resource that was assigned as #0x7f060055

Upvotes: 1

Related Questions