2015-12-31 76 views
1

使用EditaDatos按鈕功能,自動破壞方法書記官長的運作,已經工作沒有問題java.lang.NumberFormatException:無效長:「空」

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

和然後,從所述片段我使用意圖來創建活動並通過數據

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) 

回答

0

堆棧跟蹤是相當明確的。它是通過將不可解析的東西作爲long傳入Long.parseLong而引起的。這又意味着,這條線:

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

是把任一個null或字符串"null"Id變量。爲什麼這個價值不存在或者設定爲這個,我不知道。你需要計劃當價值缺席時會發生什麼;你如何恢復或失敗,你如何向用戶展示失敗?你是否展示了它? (只是崩潰是從用戶的角度來看,一個可怕的經歷。)


不過,我真的覺得這可能是更好的documento存儲作爲首位long因爲這是你真的想用什麼,使用intento.getLongExtra("documento", SOME_DEFAULT)來檢索它。你需要選擇SOME_DEFAULT;像-1這樣的哨點可能是一個好主意,只要你處理值爲缺失的情況

請記住,當用戶首次啓動應用程序或與應用程序外部的任何應用程序(包括舊版應用程序)進行互操作時,值可能會丟失。你必須防守編碼。

相關問題