2017-04-01 47 views
0

正如標題所說,當崩潰應用使用setVisibility

當使用startLabel.setVisibility(View.GONE); 該應用程序崩潰,我不知道如何修復

下面的Java代碼。

public class Main extends AppCompatActivity { 


private TextView scoreLabel; 
private TextView startLabel; 

private ImageView box; 
private ImageView orange; 
private ImageView pink; 
private ImageView black; 

// Position 
private int boxY; 
private int boxX; 

// Initialize Class 
private Handler handler = new Handler(); 
private Timer timer = new Timer(); 

//Status Check 
private boolean action_flg = false; 
private boolean start_flg = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    scoreLabel = (TextView) findViewById(R.id.scoreLabel); 
    scoreLabel = (TextView) findViewById(R.id.startLabel); 

    box = (ImageView) findViewById(R.id.box); 
    orange = (ImageView) findViewById(R.id.orange); 
    pink = (ImageView) findViewById(R.id.pink); 
    black = (ImageView) findViewById(R.id.black); 


    //Moves images to out of the screen 
    //orange.setX(-80.0f); 
    orange.setY(-80.0f); 

    // pink.setX(-80.0f); 
    pink.setY(-80.0f); 

    //black.setX(-80.0f); 
    black.setY(-80.0f); 


    boxY = 200; 

} 

public void changePos() 
{ 

    //Move box 
    if (action_flg == true) 
    { 
     // touching 
     boxY -= 20; 
    } else { 
     //released 
     boxY += 20; 

    } 
    box.setY(boxY); 
} 

public boolean onTouchEvent(MotionEvent me) 
{ 

    if (start_flg == false) 
    { 
     start_flg = true; 

     //Issue here with setting the visibility of the start 
     // startLabel.setVisibility(View.GONE); 

     timer.schedule(new TimerTask() 
     { 
      @Override 
      public void run() { 
       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         changePos(); 
        } 
       }); 
      } 

      // Changing these numbers slows down how fast the box moves. 
     }, 0, 100); 
    }else { 
     if(me.getAction() == MotionEvent.ACTION_DOWN) 
     { 
      action_flg = true; 
     }else if (me.getAction() == MotionEvent.ACTION_UP){ 
      action_flg = false; 
     } 

    } 

    return true; 
    } 
} 

這裏是XML版本的代碼。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
android:orientation="vertical" 
tools:context="me.scott.nathan.catchtheball.Main"> 



<TextView 
    android:id="@+id/scoreLabel" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:text="Score : 300" 
    android:textSize="18sp" 
    android:paddingLeft="10dp" 
    android:gravity="center_vertical"> 
    </TextView> 

    <FrameLayout 
     android:id="@+id/frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:id="@+id/startLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Tap to Start" 
      android:textSize="30sp" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="130dp" 
      /> 

     <ImageView 
      android:id="@+id/box" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:src="@drawable/box" 
      android:layout_gravity="center_vertical" 
      /> 

     <ImageView 
     android:id="@+id/orange" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:src="@drawable/orange" /> 

     <ImageView 
      android:id="@+id/black" 
      android:layout_width="24dp" 
      android:layout_height="24dp" 
      android:src="@drawable/black" /> 

     <ImageView 
      android:id="@+id/pink" 
      android:layout_width="16dp" 
      android:layout_height="16dp" 
      android:src="@drawable/pink" /> 

    </FrameLayout> 

<!-- 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.22" /> 
--> 

</LinearLayout> 

任何幫助表示讚賞來解決這個問題,謝謝

+0

將是一件好事,包括在帖子內的代碼。 –

+0

您的Java代碼是私人的 – hibob

+0

並且崩潰的堆棧跟蹤 – Vaiden

回答

0

startLabel爲空(複製粘貼問題呢?^^)。替換此:

scoreLabel = (TextView) findViewById(R.id.startLabel); 

與此:

startLabel = (TextView) findViewById(R.id.startLabel); 
+0

謝謝,這解決了這個問題。 –

相關問題