2016-08-02 18 views
0

Ich habe ein paar Posts mit dem gleichen Fehler gesehen. Keiner von ihnen hat jedoch bisher eine funktionierende Lösung für mein Problem angeboten.App stürzt beim Neustart ab

Ich habe zwei Aktivitätsklassen, MainActivity und GameActivity. MainActivity startet GameActivity bei einem Tastendruck. Wenn ich die Zurück-Taste an meinem Telefon drücke, geht es zurück zur Hauptaktivität. Allerdings, wenn ich die Taste erneut drücken, um eine neue GameActivity zu starten, bin ich aufgefordert, mit einem Fehler auf der Codezeile zeigt

setContentView(R.layout.activity_game); 

Fehler:

08-02 02:16:56.365 2220-2220/joseph.imbroglio E/AndroidRuntime: FATAL EXCEPTION: main 
Process: joseph.imbroglio, PID: 2220 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{joseph.imbroglio/joseph.imbroglio.GameActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown> 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2661) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:5835) 
    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:1399) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
    Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown> 
    at android.view.LayoutInflater.createView(LayoutInflater.java:640) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428) 
    at android.app.Activity.setContentView(Activity.java:2241) 
    at joseph.imbroglio.GameActivity.onCreate(GameActivity.java:30) 
    at android.app.Activity.performCreate(Activity.java:6221) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
    at android.app.ActivityThread.access$900(ActivityThread.java:172)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:145)  
    at android.app.ActivityThread.main(ActivityThread.java:5835)  
    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:1399)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)  
    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:614) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)  
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:511)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:366)  
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)  
    at android.app.Activity.setContentView(Activity.java:2241)  
    at joseph.imbroglio.GameActivity.onCreate(GameActivity.java:30)  
    at android.app.Activity.performCreate(Activity.java:6221)  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)  
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
    at android.app.ActivityThread.access$900(ActivityThread.java:172)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:145)  
    at android.app.ActivityThread.main(ActivityThread.java:5835)  
    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:1399)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)  
    Caused by: java.lang.OutOfMemoryError: Failed to allocate a 72000012 byte allocation with 16777216 free bytes and 65MB until OOM 
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:726) 
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:547) 
    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:575) 
    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:605) 
    at joseph.imbroglio.Entity.<init>(Entity.java:42) 
    at joseph.imbroglio.Face.<init>(Face.java:13) 
    at joseph.imbroglio.GameView.init(GameView.java:67) 
    at joseph.imbroglio.GameView.<init>(GameView.java:52) 
    at java.lang.reflect.Constructor.newInstance(Native Method)  
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288)  
    at android.view.LayoutInflater.createView(LayoutInflater.java:614)  
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)  
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:511)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:366)  
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)  
    at android.app.Activity.setContentView(Activity.java:2241)  
    at joseph.imbroglio.GameActivity.onCreate(GameActivity.java:30)  
    at android.app.Activity.performCreate(Activity.java:6221)  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)  
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
    at android.app.ActivityThread.access$900(ActivityThread.java:172)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:145)  
    at android.app.ActivityThread.main(ActivityThread.java:5835)  
    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:1399)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)  

onCreate Methode für MainActivity:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 
    mainActivity = this; 
    playbtn = (ImageView) findViewById(R.id.imageView); 
    final Intent intent = new Intent(getBaseContext(), GameActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    playbtn.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      playbtn.setImageResource(R.drawable.playbutton2); 
      startActivity(intent); 
      getIntent().removeExtra("startTest"); 
      return true; 
     } 
    }); 
} 

onCreate Methode für GameActivity:

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game); 

    gameView = (GameView)findViewById(R.id.gameView); 
    gameView.setGameActivity(this); 
    gameThread = new Thread(gameView); 
    gameThread.start(); 
    MainActivity.resetButton(); 
} 

activity_game XML-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="joseph.imbroglio.GameActivity" 
    android:layout_width="match_parent"  android:layout_height="match_parent"> 

    <joseph.imbroglio.GameView 
     android:id="@+id/gameView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/textView" 
     android:textSize="24sp" 
     android:textColor="?android:attr/colorBackground" /> 
</RelativeLayout> 

-Code für diese resetButton Methode:

public static void resetButton(){ 
    playbtn.setImageResource(R.drawable.playbutton); 
} 

Wie gewünscht, hier ist der Code für die GameView Klasse:

import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 

import java.util.ArrayList; 

/** 
* Created by root on 7/4/16. 
*/ 
public class GameView extends SurfaceView implements Runnable,  SurfaceHolder.Callback{ 

protected Resources resources; 

private SurfaceHolder holder; 

protected ArrayList<Entity> entities = new ArrayList<Entity>(); 

private long lastUpdate = 0; 

protected GameActivity ga; 

private Background background; 
private Player player; 
private Face face; 
private TileSet tileSet; 
private ArrayList<Fruit> fruits = new ArrayList<Fruit>(); 

private boolean surfaceCreated = false; 
private boolean hasActiveHolder = false; 
private boolean touchingRight = false; 
private boolean touchingLeft = false; 

private int accel = getResources().getDisplayMetrics().widthPixels/25; 

private Physics physics; 


public GameView(Context context){ 
    super(context); 
    init(); 
} 

public GameView(Context context, AttributeSet attrs){ 
    super(context, attrs); 
    init(); 
} 

public GameView(Context context, AttributeSet attrs, int defStyle){ 
    super(context, attrs, defStyle); 
    init(); 
} 

public void init(){ 
    //this.setBackgroundColor(Color.RED); 
    //setWillNotDraw(false); 
    resources = this.getResources(); 
    holder = getHolder(); 
    background = new Background(this, R.drawable.background, 1); 
    player = new Player(this, R.drawable.yellowtest, 4); 
    face = new Face(this, R.drawable.face, 8); 
    tileSet = new TileSet(this); 
    populateFruits(); 
    entities.add(background); 
    entities.add(player); 
    entities.add(face); 
    entities.addAll(tileSet.getTiles()); 
    entities.addAll(fruits); 
    holder.addCallback(this); 
    physics = new Physics(entities); 
    setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 

      if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ 
       if(motionEvent.getX() >= getResources().getDisplayMetrics().widthPixels/2){ 
        touchingRight = true; 
       } else { 
        touchingLeft = true; 
       } 
       return true; 
      } 
      if(motionEvent.getAction() == MotionEvent.ACTION_UP){ 
       touchingLeft=false; 
       touchingRight=false; 
       player.neutralize(); 
       return true; 
      } 
      return false; 
     } 
    }); 
} 

public void draw(){ 
    synchronized (this) { 
     while(!hasActiveHolder){ 
      try{ 
       this.wait(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     Canvas canvas = holder.lockCanvas(); 
     if(canvas != null) { 
      paintCanvas(canvas); 
      holder.unlockCanvasAndPost(canvas); 
     } 
    } 
} 

public void paintCanvas(Canvas canvas){ 
    for(Entity e : entities){ 
     e.draw(canvas); 
    } 
    tileSet.tileDead(fruits); 
    tileSet.checkFell(player.pos); 
    player.animation1(); 
    face.eat(); 
} 

@Override 
public void run() { 

    while(ga.playing){ 
     if((System.currentTimeMillis() - lastUpdate) < 16){ 
      try { 
       Thread.sleep(Math.abs(16 - (System.currentTimeMillis()-lastUpdate))); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     if(this.surfaceCreated) { 
      physics.update(); 
      manage(); 
      draw(); 
      lastUpdate = System.currentTimeMillis(); 
     } 

    } 
} 

@Override 
public void surfaceCreated(SurfaceHolder surfaceHolder) { 
    surfaceCreated = true; 
    synchronized (this) { 
     hasActiveHolder = true; 
     this.notifyAll(); 
    } 
} 

@Override 
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { 

} 

@Override 
public void surfaceDestroyed(SurfaceHolder surfaceHolder) { 
    surfaceCreated=false; 
    synchronized (this){ 
     hasActiveHolder = false; 

     synchronized(this){ 
      this.notifyAll(); 
     } 
    } 
} 

public int getPlayerX(){ 
    return player.x; 
} 

public int getPlayerY(){ 
    return player.y; 
} 

public Player getPlayer(){ 
    return player; 
} 

public void manage(){ 
    if(player.x > (getResources().getDisplayMetrics().widthPixels - player.width)){ 
     player.x = getResources().getDisplayMetrics().widthPixels - player.width; 
    } 
    if(player.x < 0){ 
     player.x = 0; 
    } 
    face.x = player.x; 
    //if((!touchingLeft) && (!touchingRight)){ 
     //player.ax = 0; 
    //} 
    if(player.isAlive()) { 
     if (touchingRight) { 
      player.ax += accel; 
     } 
     if (touchingLeft) { 
      player.ax -= accel; 
     } 
    } 

} 

public void populateFruits(){ 
    for(int i = 0; i < 20; i++){ 
     fruits.add(new Fruit(this, R.drawable.fruit, 2, getResources().getDisplayMetrics().widthPixels/20, getResources().getDisplayMetrics().widthPixels/20)); 
    } 
} 

public Face getFace(){ 
    return face; 
} 

public void setGameActivity(GameActivity gl){ 
    ga = gl; 
} 

}

Irgendwelche Eingaben zu diesem Problem?

+0

Geben Sie bitte den Code von 'GameView' ein. – earthw0rmjim

+0

joseph.imbroglio.GameView ?? poste den Code von GameView – SaravInfern

+0

Deine Bilder sind zu groß. Sehen Sie sich die Verwendung einer Bildladungsbibliothek an. –

Antwort

0

Unter der Annahme, dass Ihre GameActivity beginnt richtig das erste Mal, aber nach BACK drücken (zu MainActivity zurückzukehren) und dann beginnen, die GameActivity Abstürzen (wegen OutOfMemory), wahrscheinlich bedeutet, dass Sie nicht richtig und vollständig reinigen, wenn das Original Instanz von GameActivity endet.

Von dem Code, den Sie gepostet haben, haben Sie wahrscheinlich ein Speicherleck. Dieser Code:

gameView = (GameView)findViewById(R.id.gameView); 
gameView.setGameActivity(this); 
gameThread = new Thread(gameView); 
gameThread.start(); 

gibt einen Verweis auf gameView-gameThread. gameView hat einen Verweis auf die Instanz GameActivity. Wenn Sie diese Thread nicht ordnungsgemäß bereinigen, wenn die Instanz GameActivity beendet wird, kann der Garbage Collector die alte GameActivity und alle Ressourcen (einschließlich Ihrer Bilder) nicht wiederherstellen, bevor Sie versuchen, die zweite Instanz zu starten. Sie können Heap-Analyse- und Heap-Debugging-Tools verwenden, um Speicherlecks wie diese zu finden.

+0

Vielen Dank, diese Erkenntnis hat mein Problem genau gelöst. Falls es jemand anderem helfen sollte, habe ich einen booleschen onStopRequested-Wert in der onStop-Methode von GameActivity auf true gesetzt. Die run-Methode des Threads wird zurückgegeben, wenn sie erkennt, dass dieser Boolesche Wert wahr ist. – Joseph

0

Ich denke, dass die Bildgröße, die Sie für die App verwendet haben, Ihre Zelle möglicherweise nicht unterstützt. Versuchen Sie, die Größe der Bilder zu minimieren.

+0

Vielleicht haben Sie Recht, Ein häufiges Problem ist eine Nicht-Speicher-Ausnahme, wenn versucht wird, eine Bildansicht aufzuladen, die eine aufzeichnbare Ressource lädt. Wenn eine dieser Ressourcen eine hohe Pixelauflösung hat, würde viel Speicher benötigt, was dann zu einer aufgeblasenen Ausnahme führen würde. – El0din

+1

Wenn die 'GameActivity' beim ersten Mal, aber nicht beim zweiten Mal richtig geöffnet wurde, kann diese Antwort nicht richtig sein. Wie werden diese Antworten aufgewertet? Denken Sie, Leute! –