[英]Random class with polymorphism android
我正在構建一個算命的 android 應用程序,它使用隨機類從數組中選擇不同的財富,它基於多態性。 我知道我編碼得很好,但我的應用程序甚至無法打開,有什么幫助嗎? 這是我的代碼:
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);
}
}
}
您看到的問題是由於以下幾行:
Drawable blue = getResources().getDrawable(R.drawable.blue);
Drawable red = getResources().getDrawable(R.drawable.red);
在建立上下文之前,您無法實例化可繪制資源。 換句話說,此時您的 Activity 尚未創建,因此您無法調用 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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.