按钮点击给出白色屏幕,而不是去预期的活动.为什么?



我正在创建一个多用途的应用程序,其中一部分是BMI计算器。所有toast消息在模拟器和'BMI_Calculator'活动工作中的所有其他功能中正确显示。我的问题是,当我点击"计算"按钮时,屏幕变白,把我带回到我的按钮页面,如下图所示:

img1

我遵循了这个教程https://www.youtube.com/watch?v=hwlHCUiwMco&list=RDCMUCO-vqQSN1N9unBqDHfL45TA&index=3和他的所有作品。

Logcat给出这个错误:

2022-04-23 18:27:13.476 21383-21414/com.example.assignment_2 E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1

BMI_Calculator.java

public class BMI_Calculator extends AppCompatActivity {
Button CalcBMI;
TextView CurHeight, CurAge, CurWeight;
ImageView IAge, IWeight, DWeight, DAge;
SeekBar HeightSB;
RelativeLayout M, F;
int WeightIT = 55;
int AgeIT = 12;
int CurProgress;
String ProgressIT = "170";
String UserType = "0";
String Weight2 = "55";
String Age2 = "12";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi_calculator);
CalcBMI = findViewById(R.id.calculate_bmi);
CurAge = findViewById(R.id.current_age);
CurWeight = findViewById(R.id.current_weight);
CurHeight = findViewById(R.id.current_height);
IAge = findViewById(R.id.increase_age);
DAge = findViewById(R.id.decrease_age);
IWeight = findViewById(R.id.increment_weight);
DWeight = findViewById(R.id.decrease_weight);
HeightSB = findViewById(R.id.height_seek_bar);
M = findViewById(R.id.male);
F = findViewById(R.id.female);
M.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
M.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.male_female_focus));
F.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.male_female_not_focus));
UserType = "Male";
}
});
F.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
F.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.male_female_focus));
M.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.male_female_not_focus));
UserType = "Female";
}
});
HeightSB.setMax(214);
HeightSB.setProgress(170);
HeightSB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
CurProgress = progress;
ProgressIT = String.valueOf(CurProgress);
CurHeight.setText(ProgressIT);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
IAge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AgeIT=AgeIT+1;
Age2=String.valueOf(AgeIT);
CurAge.setText(Age2);
}
});
IWeight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WeightIT=WeightIT+1;
Weight2=String.valueOf(WeightIT);
CurWeight.setText(Weight2);
}
});
DWeight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WeightIT=WeightIT-1;
Weight2=String.valueOf(WeightIT);
CurWeight.setText(Weight2);
}
});
DAge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AgeIT=AgeIT-1;
Age2=String.valueOf(AgeIT);
CurAge.setText(Age2);
}
});
CalcBMI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(UserType.equals("0"))
{
Toast.makeText(getApplicationContext(),"Please Select Your Gender!",Toast.LENGTH_SHORT).show();
}
else if(ProgressIT.equals("0"))
{
Toast.makeText(getApplicationContext(),"Please Select Your Height!",Toast.LENGTH_SHORT).show();
}
else if(AgeIT==0 || AgeIT<0)
{
Toast.makeText(getApplicationContext(),"Incorrect Age. Try Again!",Toast.LENGTH_SHORT).show();
}
else if(WeightIT==0 || WeightIT<0)
{
Toast.makeText(getApplicationContext(),"Incorrect Weight. Try Again!",Toast.LENGTH_SHORT).show();
}
else
{
{ final Intent intent8 = new Intent();
intent8.putExtra("gender",UserType);
intent8.putExtra("height",ProgressIT);
intent8.putExtra("weight",Weight2);
intent8.putExtra("age",Age2);
intent8.setClass(BMI_Calculator.this, BMI_Result.class);
CalcBMI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent8);
}
});
}
}
}
});
// Initialize and assign variable
BottomNavigationView BNV = findViewById(R.id.bottom_navigation);
// Perform item selected listener
BNV.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.MindItem:
startActivity(new Intent(getApplicationContext(), Mind.class));
overridePendingTransition(0, 0);
return true;
case R.id.HomeItem:
startActivity(new Intent(getApplicationContext(), Home.class));
overridePendingTransition(0, 0);
return true;
case R.id.BodyItem:
startActivity(new Intent(getApplicationContext(), Body.class));
overridePendingTransition(0, 0);
return true;
}
return false;
}
});
Toolbar toolbar = findViewById(R.id.bmi_tb);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.TimerItem:
Intent i = new Intent(BMI_Calculator.this, Timer.class);
startActivity(i);
return true;
case R.id.SettingsItem:
Intent intent = new Intent(BMI_Calculator.this, Settings.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

BMI_Result.java

public class BMI_Result extends AppCompatActivity {
Button ReBMI;
TextView BMIDisplay, Category, G;
Intent i6, i7;
ImageView mimageview;
String BMI;
float BMI_NT;
String height;
String weight;
float heightINT, weightINT;
RelativeLayout mbackground;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi_result);
i6 = getIntent();
BMIDisplay = findViewById(R.id.bmidisplay);
Category = findViewById(R.id.bmicategory);
G = findViewById(R.id.genderdisplay);
mbackground.findViewById(R.id.contentlayout);
mimageview = findViewById(R.id.imageview);
height = i6.getStringExtra("height");
weight = i6.getStringExtra("weight");
heightINT = Float.parseFloat(height);
weightINT = Float.parseFloat(weight);
heightINT = heightINT / 100;
BMI_NT = weightINT / (heightINT * heightINT);
BMI = Float.toString(BMI_NT);
if (BMI_NT < 16) {
Category.setText("SEVERE THINNESS");
mbackground.setBackgroundColor(Color.RED);
mimageview.setImageResource(R.drawable.cross);
} else if (BMI_NT < 16.9 && BMI_NT > 16) {
Category.setText("MODERATE THINNESS");
mbackground.setBackgroundColor(Color.RED);
mimageview.setImageResource(R.drawable.warning);
} else if (BMI_NT < 18.4 && BMI_NT > 17) {
Category.setText("MILD THINNESS");
mbackground.setBackgroundColor(Color.RED);
mimageview.setImageResource(R.drawable.warning);
} else if (BMI_NT < 25 && BMI_NT > 18.4) {
Category.setText("NORMAL");
mbackground.setBackgroundColor(Color.YELLOW);
mimageview.setImageResource(R.drawable.ok);
} else if (BMI_NT < 29.4 && BMI_NT > 25) {
Category.setText("OVERWEIGHT");
mbackground.setBackgroundColor(Color.RED);
mimageview.setImageResource(R.drawable.warning);
} else {
Category.setText("OBESE");
mbackground.setBackgroundColor(Color.RED);
mimageview.setImageResource(R.drawable.warning);
}
G.setText(i6.getStringExtra("gender"));
BMIDisplay.setText(BMI);
ReBMI = findViewById(R.id.recalculate_bmi);
ReBMI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i7 = new Intent(getApplicationContext(), BMI_Calculator.class);
startActivity(i7);
}
});
}
}

我已经查过了,没有发现任何问题。请帮助。谢谢。

在您的CalcBMI。你是在内部重新声明相同的侦听器。

尝试替换CalcBMI中的else{。setOnClickListener .

else {
final Intent intent8 = new Intent(this, BMI_Result.class);
intent8.putExtra("gender", UserType);
intent8.putExtra("height", ProgressIT);
intent8.putExtra("weight", Weight2);
intent8.putExtra("age", Age2);
startActivity(intent8);
}

最新更新