2017-01-20 5 views
-1

ランダムなクラスを使って配列から別の運命を選ぶ占いアプリを作っています。多型に基づいています。私はそれをうまくコーディングしたことを知っていますが、私のアプリも開いていないでしょう、どんな助け?ここに私のコードは次のとおりです。ランダムなクラスの多型アンドロイド

public class fortuneList { 
    Random good = new Random(); 
    Random bad = new Random(); 

    public String getGood() { 
     String goodFortunes[] = new String[20]; 
     int sel = 0; 
     for (int i = 0; i < 9; i++) { 
      sel = good.nextInt(); 
     } 
     goodFortunes[0] = "Adel is awesome"; 
     goodFortunes[1] = "Adel is cool"; 
     goodFortunes[2] = "Adel is nice"; 
     goodFortunes[3] = "Adel is gentle"; 
     goodFortunes[4] = "Adel is smart"; 
     goodFortunes[5] = "Adel is rich"; 
     goodFortunes[6] = "Adel is good"; 
     goodFortunes[7] = "This is 8"; 
     goodFortunes[8] = "Change the quotes later"; 
     return goodFortunes[sel]; 
    } 

    public String getBad() { 
     String badFortunes[] = new String[5]; 
     int sele = 0; 
     for (int is = 0; is < 5; is++) { 
      sele = bad.nextInt(); 
     } 
     badFortunes[0] = "WTF"; 
     badFortunes[1] = "Bad 1"; 
     badFortunes[2] = "Nigga"; 
     badFortunes[3] = "bish"; 
     badFortunes[4] = "haha"; 
     return badFortunes[sele]; 
    } 
} 

、ここでは主な活動上の私のコードは次のとおりです。

public class MainActivity extends AppCompatActivity { 
    TextView fortuneText; 
    Random select = new Random(); 
    ImageView ball; 
    Button button; 
    Drawable blue = getResources().getDrawable(R.drawable.blue); 
    Drawable red = getResources().getDrawable(R.drawable.red); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ball = (ImageView) findViewById(R.id.ball); 
     button = (Button) findViewById(R.id.button); 
     fortuneText = (TextView) findViewById(R.id.fortuneText); 

    } 

    public void getFortune(View view){ 
     fortuneList obj = new fortuneList(); 
     String fortuneType[]={obj.getGood(),obj.getBad()}; 
     int fortuneSel = 0; 
     for(int i =0; i<2;i++){ 
      fortuneSel = select.nextInt(); 
     } 
     fortuneText.setText(fortuneType[fortuneSel]); 
     if (fortuneSel == 0) { 
      ball.setImageDrawable(blue); 
     } 
     if(fortuneSel == 1){ 
      ball.setImageDrawable(red); 
     } 
    } 
} 
+0

スタックトレースを投稿できますか? –

答えて

0

あなたが見ている問題は、これらの行は次のとおりです。

Drawable blue = getResources().getDrawable(R.drawable.blue); 
Drawable red = getResources().getDrawable(R.drawable.red); 

することができます」コンテキストを確立する前にリソースドロワブルをインスタンス化しないでください。つまり、この時点でアクティビティが作成されていないため、getResources()を呼び出すことはできません。あなたはおそらくlogcatにNullPointerExceptionを得ているでしょう。試してみてください:

public class MainActivity extends AppCompatActivity { 
    TextView fortuneText; 
    Random select = new Random(); 
    ImageView ball; 
    Button button; 
    Drawable blue; 
    Drawable red; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Instantiate AFTER your activity. 
     blue = getResources().getDrawable(R.drawable.blue); 
     red = getResources().getDrawable(R.drawable.red); 

     ball = (ImageView) findViewById(R.id.ball); 
     button = (Button) findViewById(R.id.button); 
     fortuneText = (TextView) findViewById(R.id.fortuneText); 

    } 

    public void getFortune(View view){ 
     fortuneList obj = new fortuneList(); 
     String fortuneType[]={obj.getGood(),obj.getBad()}; 
     int fortuneSel = 0; 
     for(int i =0; i<2;i++){ 
      fortuneSel = select.nextInt(); 
     } 
     fortuneText.setText(fortuneType[fortuneSel]); 
     if (fortuneSel == 0) { 
      ball.setImageDrawable(blue); 
     } 
     if(fortuneSel == 1){ 
      ball.setImageDrawable(red); 
     } 
    } 
} 
+0

こんにちは、ありがとう、アプリは実行することができますが、私がボタンをクリックするたびに、アプリは終了します。私は本当にヌル例外ポインタを理解していません –

+0

あなたのアプリをデバッグモードで実行し、Logcatの出力を見てください。あなたが持っている問題の多くは、デバッグによって簡単に修正できます。 – Jlange