【问题标题】:Random class with polymorphism android具有多态性android的随机类
【发布时间】:2017-01-20 22:27:14
【问题描述】:

我正在构建一个算命先生安卓应用程序,它使用随机类从数组中选择不同的财富,它基于多态性。我知道我编写得很好,但我的应用程序甚至无法打开,有什么帮助吗?这是我的代码:

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);
        }
    }
} 

【问题讨论】:

  • 你能发布堆栈跟踪吗?

标签: java android random polymorphism


【解决方案1】:

您看到的问题是由于以下几行:

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);
        }
    }
} 

【讨论】:

  • 您好,谢谢您,现在应用程序可以运行,但每次我单击按钮时应用程序都会关闭。我不太了解整个空异常指针
  • 您应该在调试模式下运行您的应用程序并查看 Logcat 输出。您遇到的许多问题都可以通过调试轻松解决。
猜你喜欢
  • 2020-07-31
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多