[英]Why does my activity crash when I try to get a number?
I have an activity, called AddItem
, which contains a couple fields that the user fills out and I am now trying to pass them to another activity. 我有一个名为AddItem
的活动,其中包含用户填写的几个字段,现在我试图将它们传递给另一个活动。 I was able to get the first two fields by doing this: 通过执行此操作,我能够获得前两个字段:
String messageText = ((EditText) findViewById(R.id.inputName)).getText().toString();
String discriptionText = ((EditText) findViewById(R.id.description)).getText().toString();
The above code worked fun, but then I tried to get another value which I then cast to a double like so: 上面的代码很有趣,但是后来我尝试获取另一个值,然后将其转换为double,如下所示:
double Latitude = Double.parseDouble(((EditText) findViewById(R.id.Latitude)).getText().toString());
It's kind of long and complicated but I'm basically doing the same thing with the exception of parsing the String and converting it to a double value. 这是一个漫长而复杂的过程,但是除了解析String并将其转换为double值之外,我基本上是在做同样的事情。 I determined that this is the problem code because when I comment it out the rest of the app runs fine. 我确定这是问题代码,因为当我将其注释掉时,该应用程序的其余部分运行良好。
Here is my Activity: 这是我的活动:
public class AddItem extends AppCompatActivity {
EditText inputedTask, inputedDescription, inputedLatitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
inputedTask = (EditText) findViewById(R.id.inputName);
inputedDescription = (EditText) findViewById(R.id.description);
inputedLatitude = (EditText) findViewById(R.id.Latitude);
}
public void onSaveItemButton(View view) {
String messageText = ((EditText) findViewById(R.id.inputName)).getText().toString();
String discriptionText = ((EditText) findViewById(R.id.description)).getText().toString();
double Latitude = Double.parseDouble(((EditText) findViewById(R.id.Latitude)).getText().toString());
if (messageText.equals(""));
else {
Intent intent = new Intent();
intent.putExtra(Intent_Constant.INTENT_MESSAGE_FIELD, messageText);
setResult(Intent_Constant.INTENT_RESULT_CODE, intent);
finish();
}
}
}
You need to make a public method for the onClick, from the documentation : 您需要从文档中为onClick设置一个公共方法:
Within the Activity that hosts this layout, the following method handles the click event: 在承载此布局的活动中,以下方法处理click事件:
/** Called when the user touches the button */ public void sendMessage(View view) { // Do something in response to button click }
The method you declare in the android:onClick attribute must have a signature exactly as shown above. 您在android:onClick属性中声明的方法必须具有完全如上所述的签名。 Specifically, the method must: 具体来说,该方法必须:
- Be public 公开
- Return void 返回无效
- Define a View as its only parameter (this will be the View that was clicked) 将视图定义为其唯一参数(这将是单击的视图)
So you need to change the method to public : 因此,您需要将方法更改为public :
public void onSaveItemButton(View view) {
...
}
UPDATE: 更新:
As the error log says: 如错误日志所述:
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NumberFormatException: Invalid double: "" at java.lang.StringToReal.invalidReal(StringToReal.java:63) at java.lang.StringToReal.parseDouble(StringToReal.java:267) at java.lang.Double.parseDouble(Double.java:301) at cs4720.cs.virginia.edu.duysalahandroidminiproject02.AddItem.onSaveItemButton(AddItem.java:33) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)上, 由以下原因引起:java.lang.NumberFormatException:无效的double值:“”在java.lang.StringToReal.invalidReal(StringToReal.java:63) .lang.StringToReal.parseDouble(StringToReal.java:267)at java.lang.Double.parseDouble(Double.java:301)at cs4720.cs.virginia.edu.duysalahandroidminiproject02.AddItem.onSaveItemButton(AddItem.java:33)
You need to catch for empty string in the following code: 您需要在以下代码中捕获空字符串:
double Latitude = Double.parseDouble(((EditText) findViewById(R.id.Latitude)).getText().toString());
so, check it first: 因此,请先检查一下:
String val = ((EditText) findViewById(R.id.Latitude)).getText().toString();
if(!val.equals("") {
double Latitude = Double.parseDouble(val);
}
You are calling onSaveItemButton method from a view that is not initialized. 您正在从未初始化的视图中调用onSaveItemButton方法。 You must initialize btnChangeDate in the onCreate from the activity. 您必须在活动的onCreate中初始化btnChangeDate 。
Check this answer 检查这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.