Note that there are some explanatory texts on larger screens.

plurals
  1. POupdating a text view is causing an android app to crash
    primarykey
    data
    text
    <p>I'm writing an android game where sprites come down the screen and the user taps them to remove them. When the sprite reaches the bottom of the screen the player lives decreases and the sprite is removed but the text view that displays the player lives doesn't update. </p> <p>I have put an "update" call in the on touch listener that is used to kill the sprites and this updates everything fine (player score gets increased when a sprite is killed). But I can't figure out how to get the lives to update when a sprite reaches the bottom of the screen. Every time I try to make it update when a sprite is deleted the app just crashes. I'm using (TextView).setText() to update the text view which displays the score </p> <p>The app uses openGL to render the sprites.</p> <p>How do I make it update when a sprite is deleted?</p> <p>This is the class where the screen is set up and it has the on touch listener in it too.</p> <pre><code>package com.JACSoft.game; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.TextView; public class GamePlay extends Activity { public static GameGLSurfaceView mGLView; public static int PlayerScore = 0; public static int PlayerLives = 9; public static int counter = 0; static TextView PStatsView; // creates text view to display stats static LinearLayout Stats; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Stats = new LinearLayout(this); //creates an editable layout Stats.setOrientation(1); // makes it horizontal requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); mGLView = new GameGLSurfaceView(this); PStatsView = new TextView(this); PStatsView.setTextSize(20.0f); PStatsView.setText("Score : " + PlayerScore + " " + "Lives : " + PlayerLives); // puts the string version of score in the text view Stats.addView(PStatsView); // adds views to layout Stats.addView(mGLView); // adds views to layout setContentView(Stats); // sets layout as the view } public boolean onTouchEvent(MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN : GameGLRenderer.sprites.deleteSprite(event.getX(),event.getY(), 0); PStatsView.setText("Score : " + PlayerScore + " " + "Lives : " + PlayerLives); break; } return true; } </code></pre> <p>}</p> <p>this is the bit that says I cant access the text view from a different thread, but i have tried calling a function from the sprite delete thread that is in the main thread that then calls the setText function</p> <pre><code>11-22 19:06:03.105: E/AndroidRuntime(27971): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. </code></pre> <p>this is what comes after that line </p> <pre><code>11-22 19:06:03.105: E/AndroidRuntime(27971): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4865) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:979) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4306) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.view.View.invalidate(View.java:10519) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.view.View.invalidate(View.java:10474) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.widget.TextView.checkForRelayout(TextView.java:6598) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.widget.TextView.setText(TextView.java:3705) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.widget.TextView.setText(TextView.java:3563) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.widget.TextView.setText(TextView.java:3538) 11-22 19:06:03.105: E/AndroidRuntime(27971): at com.JACSoft.game.GameSprites.deleteSprite(GameSprites.java:120) 11-22 19:06:03.105: E/AndroidRuntime(27971): at com.JACSoft.game.GameSprite.BottomReached(GameSprite.java:61) 11-22 19:06:03.105: E/AndroidRuntime(27971): at com.JACSoft.game.GameSprite.moveBallDown(GameSprite.java:51) 11-22 19:06:03.105: E/AndroidRuntime(27971): at com.JACSoft.game.GameSprite.gravitate(GameSprite.java:25) 11-22 19:06:03.105: E/AndroidRuntime(27971): at com.JACSoft.game.GameSprites.draw(GameSprites.java:60) 11-22 19:06:03.105: E/AndroidRuntime(27971): at com.JACSoft.game.GameGLRenderer.onDrawFrame(GameGLRenderer.java:68) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516) 11-22 19:06:03.105: E/AndroidRuntime(27971): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload