Android 无法在导航抽屉中计算
我一直试图在导航抽屉中制作一个计算器,但它给了我一个错误,我无法计算 如何解决此错误Android 无法在导航抽屉中计算,android,Android,我一直试图在导航抽屉中制作一个计算器,但它给了我一个错误,我无法计算 如何解决此错误 public class SNFFragment extends Fragment { public SNFFragment() { } EditText edit1, edit2; Button btn; android.widget.TextView TextView; double value1, value2; double value3; @Nullable @Override publ
public class SNFFragment extends Fragment {
public SNFFragment() {
}
EditText edit1, edit2;
Button btn;
android.widget.TextView TextView;
double value1, value2;
double value3;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.snf_layout, container, false);
edit1 = (EditText) view.findViewById(R.id.edit1);
edit2 = (EditText) view.findViewById(R.id.edit2);
btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edit1.getText().toString().isEmpty()) {
edit1.setError("Enter value1");
} else if (edit2.getText().toString().isEmpty()) {
edit2.setError("Enter value2");
} else {
calculate();
}
}
private void calculate() {
try {
value1 = Double.valueOf(edit1.getText().toString());
value2 = Double.valueOf(edit2.getText().toString());
Log.d("value1", String.valueOf(value1));
Log.d("value2", String.valueOf(value2));
value3 = (0.25 * value1 + value2 * 0.21 + 0.36);
//When the button is clicked, call the calculate method.
TextView.setText(String.format("Answer: %.2f", value3));
} catch (NumberFormatException e) {
Log.d("value1", String.valueOf(value1));
Log.d("value2", String.valueOf(value2));
Log.d("Exception", String.valueOf(e));
}
});
return view;
}
我建议您将
calculate()
函数移到onClickListener
回调函数之外,并将其作为snfffragment
的私有函数,以便您的代码如下所示:
public class SNFFragment extends Fragment {
public SNFFragment() {}
EditText edit1, edit2;
Button btn;
android.widget.TextView TextView;
double value1, value2;
double value3;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.snf_layout, container, false);
edit1 = (EditText) view.findViewById(R.id.edit1);
edit2 = (EditText) view.findViewById(R.id.edit2);
btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edit1.getText().toString().isEmpty()) {
edit1.setError("Enter value1");
} else if (edit2.getText().toString().isEmpty()) {
edit2.setError("Enter value2");
} else {
calculate();
}
}
});
return view;
}
private void calculate() {
try {
value1 = Double.valueOf(edit1.getText().toString());
value2 = Double.valueOf(edit2.getText().toString());
Log.d("value1", String.valueOf(value1));
Log.d("value2", String.valueOf(value2));
value3 = (0.25 * value1 + value2 * 0.21 + 0.36);
//When the button is clicked, call the calculate method.
TextView.setText(String.format("Answer: %.2f", value3));
} catch (NumberFormatException e) {
Log.d("value1", String.valueOf(value1));
Log.d("value2", String.valueOf(value2));
Log.d("Exception", String.valueOf(e));
}
}
}
请尝试一下,并让我们知道这是否有帮助。您遇到了什么错误?请包括错误日志。您应该将
calculate()
函数移到onCreateView
方法之外,因为它只是snffFragment类中的一个辅助方法。我得到的错误是错误:(77,9)错误:类、接口或枚举预期错误:(78,9)错误:类、接口或枚举预期错误:(79,5)错误:应为类、接口或枚举..可能的代码是什么。。。