简体   繁体   English

尝试获取电话号码时,为什么我的活动崩溃了?

[英]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.

相关问题 当我尝试运行它时,为什么我的程序会冻结/崩溃? - Why does my program freeze/crash when i try to run it? 当我尝试使用整数值填充TextView时,为什么我的应用程序崩溃? - Why does my app crash when i try to populate a TextView with an integer value? Android教程-为什么当我尝试访问现有的文本视图时我的应用程序崩溃 - Android Tutorials — Why does my app crash when I try to access an existing Text View 当我尝试访问捆绑软件中的信息时,为什么我的Android活动被强制关闭? - Why does my Android activity force close when I try to access info I put in the bundle? 当我尝试打开活动时应用程序崩溃 - Application crash when I try to open the activity 为什么开始新活动时我的android应用程序崩溃? - Why does my android application crash when starting a new activity? 当我在活动开始时点击垃圾邮件时,为什么我的 Android Studio 应用程序会崩溃? - Why does my Android studio App crash when I spam click at the beginning of an activity? 为什么当我尝试运行这个程序时它会崩溃? - Why does this program crash when I try run it? 如何在非活动类中使用活动方法? 当我尝试使用上下文时我的程序崩溃 - How to use an activity method in a non - activity class ? My program crash when I try use a context 退出我的活动时为什么会崩溃? - Why am I getting a crash when exiting my Activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM