2016-10-15 8 views
0

私のスプラッシュページのアクティビティからメインアクティビティ(ColorMatch.java)に行くと、プログラムが無期限にスタックする問題が発生しました黒い画面に表示されます。これは、私の主なアクティビティでonCreateで呼び出されたrun()メソッドが原因であることがわかりました。私はそれが動作するかどうかを確認するために特定のものをコメントアウトしようとしましたが、run()がonCreateで呼び出される限り、自動的に黒い画面になります。この問題の原因は何ですか?Android - アクティビティのメインメソッドはプログラムを黒い画面にクラッシュさせますが、エラーは発生しません

マイClassMatch.java:それは使用しています

package com.example.ali.colormatch; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import java.util.Random; 

public class ColorMatch extends AppCompatActivity { 

int answer; //1 = red, 2 = blue, 3 = green, 4 = yellow 
TextView textView3, textView2; 
int count = 0; 
volatile boolean playing = true; 
private long timeThisFrame; 
private Button testButton; 
long fps; 
int score; 
int correctAnswer; 
boolean matchColor = true, matchText = false; 
long startFrameTime; 
boolean firstTime = true; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_color_match); 
    textView3 = (TextView) findViewById(R.id.textView3); 
    textView2 = (TextView) findViewById(R.id.textView2); 

    run(); 
} 

public void run() { 
    while (playing) { 

     startFrameTime = System.currentTimeMillis(); 

     if (firstTime){ 
      generateNewWord(); 
      firstTime = false; 
     } 

     if (answer == correctAnswer){ 
      score++; 
      count++; 
      generateNewWord();} 
     //else { 
     // quit(); 
     // } 


     if (count % 5 == 0){ 
       if (matchColor) { 
        textView3.setText(getString(R.string.gameSetting2)); // might need to add context with this - check http://stackoverflow.com/questions/10698945/reference-string-resource-from-code 
        matchColor = true; 
        matchText = false; 
       } 
       else if (matchText){ 
        textView3.setText(getString(R.string.gameSetting1)); 
        matchText = true; 
        matchColor = false; 
       } 
      } 
     } 

     //draw(); 
     timeThisFrame = System.currentTimeMillis() - startFrameTime; 
     if (timeThisFrame > 0) { 
      fps = 1000/timeThisFrame; 
     } 

    } 

public void generateNewWord(){ 

    //randomly select between red, green, blue, yellow 
    Random rand = new Random(); 
    int randomInt1 = rand.nextInt(4) + 1; // assigns randomInt a value between 1 - 4 
    int randomInt2 = rand.nextInt(4) + 1; 

    if (randomInt1 ==1){ 
     textView3.setText(R.string.Red); 
    } 
    else if (randomInt1 ==2){ 
     textView3.setText(R.string.Green); 
    } 
    else if (randomInt1 == 3){ 
     textView3.setText(R.string.Blue); 
    } 
    else if (randomInt1 == 4){ 
     textView3.setText(R.string.Yellow); 
    } 


    //randomly select hex codes between rgby 
    if (randomInt2 ==1){ 
     textView3.setTextColor(0xffcc0000); 
    } 
    else if (randomInt2 ==2){ 
     textView3.setTextColor(0xff669900); 
    } 
    else if (randomInt2 == 3){ 
     textView3.setTextColor(0xff000080); 
    } 
    else if (randomInt2 == 4){ 
     textView3.setTextColor(0xffffff00); 
    } 


    if (matchColor) { 
     correctAnswer = randomInt2; 
    } 
    else if(matchText){ 
     correctAnswer = randomInt1; 
    } 

} 
public void quit(){ 

} 


public void sendBlue(View view){ 
    answer = 2; 
} 
public void sendRed(View view){ 
    answer = 1; 
} 
public void sendYellow(View view){ 
    answer = 4; 
} 
public void sendGreen(View view){ 
    answer = 3; 
} 


} 

activity_color_match.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" 
android:id="@+id/activity_color_match" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.ali.colormatch.ColorMatch"> 

<TextView 

    android:text="@string/matchText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView3" 
    android:textAppearance="@style/TextAppearance.AppCompat.Body2" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:text="@string/mainColor" 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:textColor="@android:color/background_dark" 
    android:textSize="100dp" 
    android:layout_marginBottom="104dp" 
    android:textAppearance="@style/TextAppearance.AppCompat.Display3" 
    android:layout_above="@+id/button3" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:layout_width="150dp" 
    android:layout_height="60dp" 
    android:id="@+id/button" 
    android:background="#000080" 
    android:onClick="sendBlue" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<Button 
    android:layout_height="60dp" 
    android:id="@+id/button2" 
    android:background="@color/yellow" 
    android:elevation="0dp" 
    android:layout_width="150dp" 
    android:onClick="sendYellow" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<Button 
    android:layout_width="150dp" 
    android:layout_height="60dp" 
    android:id="@+id/button3" 
    android:background="@android:color/holo_red_dark" 
    android:onClick="sendRed" 
    android:layout_above="@+id/button" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginBottom="19dp" /> 

<Button 
    android:layout_width="150dp" 
    android:layout_height="60dp" 
    android:id="@+id/button4" 
    android:background="@android:color/holo_green_dark" 
    android:onClick="sendGreen" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignBottom="@+id/button3" /> 


</RelativeLayout> 

完了のために、私はそれはおそらく無関係だと思うけれども、ここに私MainActivity.javaです。私が希望として動作しているようだ:

package com.example.ali.colormatch; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 


public class MainActivity extends AppCompatActivity { 

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

public void onSplashPageClick(View view){ 
    Intent intent = new Intent(MainActivity.this, ColorMatch.class); 
    startActivity(intent); 
} 

public void onQuitClick(View view){ 
    finish(); 
} 
} 

私は私のアプリとデバッグを起動するネクサス5を使用しています。ありがとうございました。

+0

'onCreate()'は 'run()'メソッドの 'playing'が' true'を返さないので、無限ループになります。 'onCreate()'は 'Activity'のライフサイクルメソッドの残りの部分を返すことができないので、呼び出すことはできません。その結果、 'onResume()'は返されません。UIは描画されず、黒い画面が表示されます。バックグラウンドスレッドで 'while'ループロジックを移動し、結果をメインスレッドにポストする必要があります。 – Onik

+0

あなたは正しいです!私はwhile(playing)ループを無効にして動作しました。これは初めてのAndroidでのJavaでの経験しかないので、私のループは私が問題になる可能性があるとは思わなかった基礎でした。私はそれを彷彿とさせています(再生中)現在、現在はテキストがロードされるので、リフレッシュする方法を見つける必要があります。 – Johnny

答えて

0

あなたはonCreate()でスレッドを作成し、このスレッドにメソッド 'run()'を入れることができると思います。

はそれを試してください:

  new Thread(new Runnable() { 
       @Override 
       public void run() { 
        ColorMatch.this.run(); 
       } 
      }).start(); 

**編集**

の理由は、あなたがUIスレッド(メインスレッド)のonCreate、そしてあなたはあまりにも多くの時間を無駄場合にあまりにも多くのことを行うですonCreateメソッドでは、ANR(Activity Not Responding)になりますので、時間を無駄にするタスクをバックグラウンドスレッドに入れる必要があります。そして、それは活動onCreateのときに黒い画面を避けるでしょう。

+0

'AsyncTask'は、少なくとも比較的短い操作で、Androidでこれを行う標準的な方法です。 – nasch

+0

彼はUIスレッドをブロックしません。まだ実行中です。また、 'run()'メソッドウィジェットでは「タッチ」されているので、あなたの提案は失敗します。 – Onik

+0

はいAsyncTaskは標準的な方法です:)。 –

0

ちょっと今でもビューを更新する方法をお探しの場合はタイマーを使用してください。

ここでこの回答をご覧ください。https://stackoverflow.com/a/18353754/2311117タイマーの使い方を説明しています。

TimerおよびTimerTaskの詳細については、

+0

ユーザーがボタンをクリックする時間を制限するタイマーを追加する予定です。そのため、Timerを使用できるようです。しかし、ユーザーがボタンをクリックするたびにrun()メソッドが必要になります。答えが正しければ、新しい単語を生成し、タイマーをリフレッシュします。間違っていると、プログラムは停止します。 – Johnny

+0

タイマーを開始/停止するには 'Timer.start'と' Timer.cancel'を使用できます。限り、私はこれを理解する限り、ユーザーが何らかのアクションを取るまで視界をリフレッシュする何かが必要です。 – Shivansh

関連する問題