2016-08-02 154 views
0

我已经看到几个帖子出现这个错误。但是,他们还没有提供解决我的问题的解决方案。应用程序在重新启动活动时崩溃

我有两个活动类,MainActivity和GameActivity。 MainActivity在按下按钮时启动GameActivity。如果我按下手机上的后退按钮,它会回到主要活动。然而,当我再次按下按钮,启动一个新的GameActivity,我提示指向的代码行的错误

setContentView(R.layout.activity_game); 

错误:

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)  

为MainActivity onCreate方法:

@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; 
     } 
    }); 
} 
为GameActivity

onCreate方法:

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文件:

<?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> 

代码为resetButton方法:

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

按照要求,这里的GameView类的代码:

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; 
} 

}

关于这个问题的任何输入?

+0

请发布'GameView'的代码。 – earthw0rmjim

+0

joseph.imbroglio.GameView ??发布GameView代码 – SaravInfern

+0

您的图片太大。看看使用图像加载库 –

回答

0

假设你GameActivity开始正确的第一次,但按BACK后(返回MainActivity),然后开始GameActivity崩溃(由于内存不足),可能意味着你是不是完全正确清理时,原GameActivity的实例结束。

从您发布的代码中,您可能有内存泄漏。此代码:

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

传递一个参考gameViewgameThreadgameView有一个参考GameActivity的实例。如果在GameActivity实例关闭时没有正确地清除此Thread,则在尝试启动第二个实例之前,垃圾回收器将无法回收旧的GameActivity及其所有资源(包括您的图像)。您可以使用堆分析和堆调试工具来帮助您找到像这样的内存泄漏。

+0

非常感谢,这种见解完全解决了我的问题。如果对别人有帮助,我在GameActivity的onStop方法中创建了一个onStopRequested布尔值设置为true。当线程的run方法看到这个布尔值为真时,它会返回。 – Joseph

0

我认为您用于该应用的图片大小可能不支持您的手机。尽量减少图像的大小。

+0

也许你是对的,一个常见的问题是当尝试膨胀一个imageview加载可绘制资源时,内存不足异常。如果其中一个资源具有较高的像素分辨率,则会占用大量内存,从而导致膨胀异常。 – El0din

+1

如果GameActivity第一次正确打开,但不是第二次,这个答案是不对的。这些答案如何得到upvotes?想想,人! –

相关问题