我有这个非常基本的应用程序,我试图随机显示骰子的图像。在这个阶段,我试图弄清楚为什么在新活动中不会一直显示骰子。当我从微调器开始第二个活动时出现问题,有时我可以多次启动第二个活动并且它工作正常,然后突然骰子消失了。这只发生在我尝试在主要活动中从微调器滚动骰子时,而不是在第二个活动中按下按钮时。
我希望有人可以帮助我!
主要活动
package com.example.t.diceapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.NmbrOfDiceArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (spinner.getSelectedItemPosition() == 1) {
startActivity(new Intent(MainActivity.this, OneDiceActivity.class));
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onResume() {
super.onResume();
spinner.setSelection(0);
}
}
第二项活动
package com.example.t.diceapplication;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
public class OneDiceActivity extends AppCompatActivity {
ImageView imageView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_dice);
imageView=findViewById(R.id.imageView2);
button=findViewById(R.id.button);
DisplayDice();
}
public void DisplayDice(){
if (getRandomNumber()==1){
imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable._diceone));
}
else if(getRandomNumber()==2){
imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable._dicetwo));
}
else if (getRandomNumber()==3){
imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable._dicethree));
}
else if (getRandomNumber()==4){
imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable._dicefour));
}
else if (getRandomNumber()==5){
imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable._dicefive));
}
else if (getRandomNumber()==6){
imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable._dicesix));
}
}
public void rollDiceOnButtonClick(View view){
DisplayDice();
}
public int getRandomNumber(){
Random r = new Random();
int Low = 1;
int High = 6;
return r.nextInt((High-Low)+1) + Low;
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
}
答案 0 :(得分:0)
我认为您的if
逻辑存在问题。你得到的每个条件都有一个随机数,这可能导致你的if
个分支都没有。试试这个。您只需要获取一次随机数。
public void DisplayDice(){
int diceRandom = getRandomNumber();
// replace other instances to this method call with the variable
if (diceRandom ==1){
imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable._diceone));
}
....
}