我正在做一个简单的项目,作为我的移动开发类的一部分。然而,我的项目运行良好,在为横向视图创建活动后,两个活动都停止触发java代码。我从项目中删除了layout land文件夹,但我最初的布局活动仍然没有触发java代码。
我怀疑两者之间的联系已经断了,但eclipse并没有将其报告为错误。setContentView()函数调用正确的xml,工具:context调用正确的java。我错过了什么?
这是onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
// assign objects to java variables
teValue1 = (EditText)findViewById(R.id.teValue1);
teValue2 = (EditText)findViewById(R.id.teValue2);
tvResult = (TextView)findViewById(R.id.tvResult);
btPlus = (Button)findViewById(R.id.btPlus);
btMinus = (Button)findViewById(R.id.btMinus);
btMultiply = (Button)findViewById(R.id.btMultiply);
btDivide = (Button)findViewById(R.id.btDivide);
btAbout = (Button)findViewById(R.id.btAbout);
// Add on click listeners
btPlus.setOnClickListener(new Add());
btMinus.setOnClickListener(new Subtract());
btMultiply.setOnClickListener(new Multiply());
btDivide.setOnClickListener(new Divide());
}
点击监听器之一:
private class Add implements OnClickListener {
@Override
public void onClick(View view) {
if (!teValue1.getText().toString().matches("")
&& !teValue1.getText().toString().matches(".")
&& !teValue2.getText().toString().matches("")
&& !teValue2.getText().toString().matches(".")) {
double operand1 = Double.parseDouble(teValue1.getText()
.toString());
double operand2 = Double.parseDouble(teValue2.getText()
.toString());
Double result = operand1 + operand2;
tvResult.setText(result.toString());
}
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cs325.matheson.calculator.CalculatorActivity" >
// UI items are here...
<requestFocus />
</RelativeLayout>
onClick方法中的if条件从未返回true。我修改了它们,现在它起作用了。让我困惑的是,为什么他们以前工作过,后来又停止了工作。