按下Computecost
按钮
package com.example.hw_3;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
public class ShoppingExpensesPage extends Activity
{
TextView et;
Button computecost;
Button save;
Button cancel;
RadioGroup drinks;
RadioButton drink;
int tottalcost=0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
et=(TextView)findViewById(R.id.tv11);
Bundle extra = getIntent().getExtras();
String val1 = extra.getString("value");
et.setText(val1);
computecost=(Button)findViewById(R.id.btn11);
computecost.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String val;
int selectedId = drinks.getCheckedRadioButtonId();
drink = (RadioButton)findViewById(selectedId);
val=(String) drink.getText();
if(val=="Juice")
{tottalcost=tottalcost+3;
}
else if (val=="Cola")
{
tottalcost=tottalcost+2;
}
et.setText(Integer.toString(tottalcost));
}
});
save=(Button)findViewById(R.id.btn21);
save.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
String str=Integer.toString(tottalcost);//(String) et.getText();
returnIntent.putExtra("return", str);
setResult(RESULT_OK,returnIntent);
finish();
}
});
}
}
答案 0 :(得分:4)
您尚未设置drinks
的视图。
int selectedId = drinks.getCheckedRadioButtonId();
在computecost.setOnClickListener
之前找到它的视图:
drinks = (RadioGroup) findViewById(...);
答案 1 :(得分:1)
这是比较Strings
if(val=="Juice")
在Java中,“==”比较Objects
的引用,但不比较它们的值。您需要使用.equals()
if("Juice".equals(val))
{ // do something }
如果这不能解决您的问题,那么请从错误中发布logcat,但仍需要更改。