java.lang.NumberFormatException:无效的long:“null”

时间:2015-12-31 18:11:53

标签: android android-fragments

使用EditaDatos按钮功能会自动损坏已经正常工作的Registrar方法的功能

MyDB.java

public void addPersona(Personas personas) {
    ContentValues values = new ContentValues();
    values.put(DBTables.EntradasTablas.COLUMN_DOCUMENTO, personas.getDocumento());
    values.put(DBTables.EntradasTablas.COLUMN_NOMBRE, personas.getNombre());
    values.put(DBTables.EntradasTablas.COLUMN_DIRECCION, personas.getDireccion());
    values.put(DBTables.EntradasTablas.COLUMN_TEL, personas.getTel());
    values.put(DBTables.EntradasTablas.COLUMN_EMAIL, personas.getEmail());
    values.put(DBTables.EntradasTablas.COLUMN_PASS, personas.getPass());

    SQLiteDatabase db = getWritableDatabase();
    db.insert(DBTables.EntradasTablas.TABLA_NOMBRE, null, values);
    db.close();

}

public Cursor personaPorId(long id) {
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + DBTables.EntradasTablas.TABLA_NOMBRE + " WHERE " + DBTables.EntradasTablas.COLUMN_DOCUMENTO + " = " + id + ";";
    Cursor c = db.rawQuery(query, null);

    if (c != null) {
        c.moveToFirst();
    }

    return c;
}

然后,从片段我使用Intent创建活动并传递数据

Cuenta.java

public class Cuenta extends android.support.v4.app.Fragment {

    EditText EtEditDocu;
    Button EditaDatos;

    View vista;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        vista = inflater.inflate(R.layout.fragment_cuenta, container, false);
        EtEditDocu = (EditText) vista.findViewById(R.id.EtEditDocu);
        EditaDatos = (Button) vista.findViewById(R.id.EditaDatos);

        Typeface Titulos = Typeface.createFromAsset(getActivity().getAssets(), "fonts/EaDesignerBold.ttf");
        TituloActualizar.setTypeface(Titulos);

        EditaDatos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intento = new Intent(getActivity(), Registro.class);
                EtEditDocu = (EditText) vista.findViewById(R.id.EtEditDocu);
                intento.putExtra("documento", EtEditDocu.getText().toString());
                startActivity(intento);
            }
        });

        return vista;
    }
}

Registro.java

public class Registro extends ActionBarActivity {

    MyDB MyDB;
    TextView Registrate;
    EditText EtDocuReg;
    EditText EtNombReg;
    EditText EtDirecReg;
    EditText EtTelReg;
    EditText EtEmailReg;
    EditText EtPassReg;
    Button Registrar;
    int idglobal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registro);
        Registrate = (TextView) findViewById(R.id.Registrate);
        EtDocuReg = (EditText) findViewById(R.id.EtDocuReg);
        EtNombReg = (EditText) findViewById(R.id.EtNombReg);
        EtDirecReg = (EditText) findViewById(R.id.EtDirecReg);
        EtTelReg = (EditText) findViewById(R.id.EtTelReg);
        EtEmailReg = (EditText) findViewById(R.id.EtEmailReg);
        EtPassReg = (EditText) findViewById(R.id.EtPassReg);

        MyDB = new MyDB(this, null, null, 1);
        Personas persona = new Personas();
        Intent intento = getIntent(); // intent creado anteriormente
        String Id = intento.getStringExtra("documento");
        long Documento = Long.parseLong(Id);
        Cursor c = MyDB.personaPorId(Documento);
        EtDocuReg.setText(c.getString(c.getColumnIndexOrThrow("documento")));
        EtNombReg.setText(c.getString(c.getColumnIndexOrThrow("nombre")));
        EtDirecReg.setText(c.getString(c.getColumnIndexOrThrow("direccion")));
        EtTelReg.setText(c.getString(c.getColumnIndexOrThrow("telefono")));
        EtEmailReg.setText(c.getString(c.getColumnIndexOrThrow("email")));
        EtPassReg.setText(c.getString(c.getColumnIndexOrThrow("contraseña")));
        idglobal = c.getInt(c.getColumnIndexOrThrow("_id"));

        Typeface Titulos = Typeface.createFromAsset(Registrate.getContext().getAssets(), "fonts/EaDesignerBold.ttf");
        Registrate.setTypeface(Titulos);
    }

    public void Registrar(View view) {
        Personas usuario = new Personas(Integer.parseInt(EtDocuReg.getText().toString()), EtNombReg.getText().toString(), EtDirecReg.getText().toString(), EtTelReg.getText().toString(), EtEmailReg.getText().toString(), EtPassReg.getText().toString());
        MyDB.addPersona(usuario);
        limpiarCampos();
        Confirmacion();
    }

    public void limpiarCampos() {
        EtDocuReg.setText("");
        EtNombReg.setText("");
        EtDirecReg.setText("");
        EtTelReg.setText("");
        EtEmailReg.setText("");
        EtPassReg.setText("");
    }

    public void Confirmacion() {
        AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
        dlgAlert.setMessage("Se ha agregado exitosamente!");
        dlgAlert.setTitle("Agregar Persona");

        dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //dismiss the dialog
            }
        });

        dlgAlert.setCancelable(true);
        dlgAlert.create().show();
    }
}

这是错误

12-31 13:10:42.426  20428-20428/com.example.leonardo.odontoexpress E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.leonardo.odontoexpress/com.example.leonardo.odontoexpress.Registro}: java.lang.NumberFormatException: Invalid long: "null"
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5392)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NumberFormatException: Invalid long: "null"
            at java.lang.Long.invalidLong(Long.java:125)
            at java.lang.Long.parseLong(Long.java:342)
            at java.lang.Long.parseLong(Long.java:319)
            at com.example.leonardo.odontoexpress.Registro.onCreate(Registro.java:49)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5392)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

堆栈跟踪相当清晰。将long无法解析的内容传递给Long.parseLong会导致这种情况。这反过来意味着这一行:

String Id = intento.getStringExtra("documento");

null或字符串"null"放入Id变量。为什么价值不存在或设定为那个,我不知道。你需要计划当价值不存在时会发生什么;你如何恢复或失败,你如何向用户呈现失败?你介绍过吗? (从用户的角度来看,崩溃是一种糟糕的体验。)

但是,我真的认为最好将documento作为long存储为您真正想要使用的内容,然后使用{{1}检索它。您需要选择intento.getLongExtra("documento", SOME_DEFAULT)的内容;像SOME_DEFAULT这样的哨兵可能是一个好主意,只要你处理缺少值时发生的事情

请记住,当用户首次启动您的应用程序时,或者当您与应用程序之外的任何内容(包括应用程序的旧版本)进行互操作时,特别可能缺少值。你必须在防御性方面进行编码。