api的问题

时间:2018-04-15 20:34:12

标签: android xml api android-studio

我在android studio中尝试了一些东西,我意识到它不能在Android 5.0或6.0上运行,它只能在7.0上运行。我正在使用compileSdkVersion 26,minSdkVersion 21和targetSdkVersion 26所以它应该没有任何问题。我不明白logcat的错误。有人可以帮帮我吗?

由于

这是代码:

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) 

1 个答案:

答案 0 :(得分:1)

查看堆栈跟踪,这似乎是实际问题:

  

引起:android.content.res.Resources $ NotFoundException:资源ID#0x7f060055

可能由以下原因引起:

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

你应该可以检查我的开场&#34; R.java&#34;并寻找分配为#0x7f060055

的资源