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

时间:2016-11-15 07:24:55

标签: android android-intent arraylist

我有一个复选框列表,如果选中,则会将星期几添加到数组列表中。我想将该arraylist传递给另一个类,如sendTo()

中所示

这是我在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);
}
}

和我的调试输出:

 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代码:

 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);
}
}

编辑:我认为问题来自这些代码行

 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);
        }
    });

特别是我解析startTime,它以HH:MM格式作为字符串读入。将其更改为字符串值而不是int,看看会发生什么。

3 个答案:

答案 0 :(得分:3)

您不能将空字符串放入数字数据,如果为空,则应为0。

答案 1 :(得分:2)

您正在获取数字格式异常,因为您正在解析&#34;&#34;空白字符串。请在解析之前检查字符串是否为空。

在解析之前,请确保它不应为空或null。为避免此异常,请正确处理try catch块或使字符串&#34; 0&#34;的默认值。

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

答案 2 :(得分:1)

而不是intent.putStringArrayListExtra("days", days) 试试这个intent.putExtra("days", days)