简体   繁体   English

无法启动活动ComponentInfo:java.lang.NumberFormatException:对于输入字符串:“”

[英]Unable to start activity ComponentInfo: java.lang.NumberFormatException: For input string: “”

I have a list of check boxes and if checked, they add the day of the week to an array list. 我有一个复选框列表,如果选中,它们会将星期几添加到数组列表中。 I want to pass that arraylist to another class as seen in the sendTo() 我想将该数组列表传递给另一个类,如sendTo()所示。

here is my code from the Sel_Dates class: 这是我的Sel_Dates类的代码:

 package com.example.joshpc.bluetoothattendee;

 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.CheckBox;

 import java.util.ArrayList;

public class Sel_Dates extends AppCompatActivity {
CheckBox mon;
CheckBox tues;
CheckBox wed;
CheckBox thur;
CheckBox fri;
Button selectBut;
ArrayList<String> days = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sel_dates);

    mon = (CheckBox) findViewById(R.id.cbMon);
    tues = (CheckBox) findViewById(R.id.cbTues);
    wed = (CheckBox) findViewById(R.id.cbWed);
    thur = (CheckBox) findViewById(R.id.cbThurs);
    fri = (CheckBox) findViewById(R.id.cbFri);
    selectBut  = (Button) findViewById(R.id.bSelectDate);

    selectBut.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            getDates();
            sendTo();
        }
    });
}

private void getDates(){
    if(mon.isChecked())
        days.add("monday");
    if(tues.isChecked())
        days.add("tuesday");
    if(wed.isChecked())
        days.add("wednesday");
    if(thur.isChecked())
        days.add("thursday");
    if(fri.isChecked())
        days.add("friday");
}
private void sendTo(){
    Intent intent = new Intent(this, Create_Class.class);
    intent.putStringArrayListExtra("days", days);
    startActivity(intent);
}
}

and my debug output: 和我的调试输出:

 FATAL EXCEPTION: main
              Process: com.example.joshpc.bluetoothattendee, PID: 2318
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joshpc.bluetoothattendee/com.example.joshpc.bluetoothattendee.Create_Class}: java.lang.NumberFormatException: For input string: ""
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
               Caused by: java.lang.NumberFormatException: For input string: ""
                  at java.lang.Integer.parseInt(Integer.java:533)
                  at java.lang.Integer.parseInt(Integer.java:556)
                  at com.example.joshpc.bluetoothattendee.Create_Class.onCreate(Create_Class.java:38)
                  at android.app.Activity.performCreate(Activity.java:6679)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6119) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Create_Class code: Create_Class代码:

 package com.example.joshpc.bluetoothattendee;

 import android.content.Intent;
 import android.provider.ContactsContract;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;

 import com.google.firebase.auth.FirebaseAuth;
 import com.google.firebase.auth.FirebaseUser;
 import com.google.firebase.database.DatabaseReference;
 import com.google.firebase.database.FirebaseDatabase;

 import java.util.ArrayList;

 public class Create_Class extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_class);

    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    final DatabaseReference fbRef = FirebaseDatabase.getInstance().getReference();

    Button createBut = (Button) findViewById(R.id.bCreate);
    EditText etstartTime = (EditText) findViewById(R.id.etStartTime);
    EditText etclassID = (EditText) findViewById(R.id.etID);
    EditText etname = (EditText) findViewById(R.id.etClassName);

    final ArrayList<String> days = getIntent().getStringArrayListExtra("days");
    final String teacherID = user.getUid();
    final String name = etname.getText().toString();
    final String classID = etclassID.getText().toString();
    final int startTime = Integer.parseInt(etstartTime.getText().toString());

    createBut.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            ClassObj newClass = new ClassObj(name, classID, teacherID, startTime, days);
            fbRef.child(user.getUid()).setValue(newClass);
            sendTo(Main__Menu.class);
        }
    });

}

private void sendTo(Class s){
    Intent intent = new Intent(this, s);
    startActivity(intent);
}
}

edit: I think the problem is coming from these lines of code 编辑:我认为问题来自这些代码行

 final int startTime = Integer.parseInt(etstartTime.getText().toString());

    createBut.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            ClassObj newClass = new ClassObj(name, classID, teacherID, startTime, days);
            fbRef.child(user.getUid()).setValue(newClass);
            sendTo(Main__Menu.class);
        }
    });

specifically where I parse startTime which is read in as a string in the format HH:MM. 特别是在我解析startTime的地方,该startTime以HH:MM格式作为字符串读取。 going to change this to a string value rather than an int and see what happens. 将其更改为字符串值而不是int,然后看看会发生什么。

您不能将空字符串添加到数字数据中,如果为空则应为0。

You are getting number format exception because you are parsing "" blank string. 由于解析“”空字符串,因此出现数字格式异常。 Please check string should not be blank before parsing it. 解析前,请检查字符串是否为空。

Please make sure it should not be blank or null before parsing it. 在解析之前,请确保它不能为空或为null。 To avoid this exception please properly handle try catch block or make default value of string "0". 为避免此异常,请正确处理try catch块或将字符串的默认值设置为“ 0”。

String str="0";
   try {
        int val=Integer.parseInt(str);
   }catch (NumberFormatException e){
       System.out.println("not a number"); 
   } 

代替intent.putStringArrayListExtra("days", days)尝试此intent.putExtra("days", days)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.RuntimeException:无法启动活动ComponentInfo:java.lang.NumberFormatException:对于输入字符串:“ 4.90” - java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NumberFormatException: For input string: “4.90” 我该如何解决:java.lang.RuntimeException:无法启动活动ComponentInfo java.lang.NumberFormatException:对于输入字符串:“ s%”? - How do I solve: java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.NumberFormatException: For input string: “s%”? java.lang.NumberFormatException: 对于输入字符串:&quot;something&quot; - java.lang.NumberFormatException: For input string:"something" java.lang.NumberFormatException:对于输入字符串:“5.3” - java.lang.NumberFormatException: For input string: "5.3" java.lang.NumberFormatException:对于输入字符串:“1538956627792” - java.lang.NumberFormatException: For input string: "1538956627792" java.lang.NumberFormatException:无法将String解析为整数 - java.lang.NumberFormatException: unable to parse String as integer java.lang.NumberFormatException:对于输入字符串:“2019-11-27” - java.lang.NumberFormatException: For input string: “2019-11-27” java.lang.NumberFormatException:对于输入字符串:“21:30” - java.lang.NumberFormatException: For input string: "21:30" 错误 java.lang.NumberFormatException: 对于输入字符串,程序崩溃 - Error java.lang.NumberFormatException: For input string, program crash java.lang.NumberFormatException:对于输入字符串:“-0。086167157” - java.lang.NumberFormatException: For input string: “-0.‎086167157”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM