【发布时间】:2015-02-18 23:22:03
【问题描述】:
我正在尝试为我所在的类创建一个 Cab 应用程序,并在 MainActivity.java 中添加了这个。我遇到的问题是当我到达“final TextView result=(TextView)findViewById(R.id.txtResult);”行时这是对 Android 应用程序开发课程的介绍,为什么 Java 不是必需的,这超出了我的理解,但事实就是如此,我对自己正在做的事情更有信心,但我似乎无法弄清楚为什么这条线不工作。我在 txtResult 的 strings.xml 文件中确实有一个字符串设置。如果有人能提供有关我可能错过的内容的见解,将不胜感激。
public class MainActivity extends ActionBarActivity {
//Declarations
double costPerMile=3.25;
int numberOfMiles;
double baseCost = 3.00;
double totalCost;
String carChoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText miles=(EditText)findViewById(R.id.txtdistance);
final Spinner group=(Spinner)findViewById(R.array.Cab_Type);
Button cost= (Button)findViewById(R.id.btnSubmit);
final TextView result=(TextView)findViewById(R.id.txtResult);
cost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Todo
numberOfMiles = Integer.parseInt(miles.getText().toString());
totalCost = (costPerMile * numberOfMiles) + baseCost;
DecimalFormat currency = new DecimalFormat("$###,###.##");
carChoice = group.getSelectedItem().toString();
result.setText("You chose the " + carChoice + " which will cost you " + currency.format(totalCost));
}
});
}
【问题讨论】:
-
更详细地解释问题。 “不工作”是不够的。如果它崩溃,请检查 logcat。您可能想学习如何读取堆栈跟踪(虽然它非常简单,所以不用担心)
-
尝试运行应用程序时收到错误消息。错误:(33, 58) 错误:找不到符号变量 txtResult 错误:任务 ':app:compileDebugJava' 执行失败。 > 编译失败;有关详细信息,请参阅编译器错误输出。
-
“那个”错误?什么错误? Nvm 你编辑的。不过,请编辑问题。