2012-07-06 57 views
0

我在android模擬器上出現此錯誤。 我正在使用它來從用戶獲得int值:它適用於字符串值,但與int值無關。嘗試parseInt()也沒有運氣。Android中的Java空指針錯誤

public void btnAddTest(View view) 
    { 
    dbHelper= new DatabaseHelper(this);  
    boolean ok=true; 
    try 
    {   String tname=test.getText().toString(); 
    String Unit=unit.getText().toString(); 
     Spannable spn=a.getText(); 
     Spannable spn1=f.getText(); 
     Spannable spn2=t.getText(); 
     Spannable _aft=aft.getText(); 
     Spannable x2=calx2.getText(); 
     Spannable _x=calx.getText(); 
     Spannable val=num.getText(); 


     int avalue=Integer.parseInt(spn.toString()); 
     int fvalue=Integer.parseInt(spn1.toString()); 
     int tvalue=Integer.parseInt(spn2.toString()); 
     int AFTID=Integer.parseInt(_aft.toString()); 
     int num1=Integer.parseInt(x2.toString()); 

     int num2=Integer.parseInt(_x.toString()); 

     int num3=Integer.parseInt(val.toString()); 
//  process p=new process(AFTID,avalue,fvalue,tvalue); 

     testname t= new testname(tname,Unit,num1,num2,num3,AFTID); 
     setTitle("Processing"); 
     dbHelper.testname(t); 
    // dbHelper.process(p); 

    } 
    catch(Exception ex) 
    { 
     ok=false; 
     CatchError(ex.toString()); 
    } 
    finally 
    { 
     if(ok) 
     { 

// NotifyEmpAdded(); class1.ShowAlert2(this);

 } 
    }} 
void CatchError(String Exception) 
{ 
    Dialog diag=new Dialog(this); 
    diag.setTitle("Add new Test"); 
    TextView txt=new TextView(this); 
    txt.setText(Exception); 
    diag.setContentView(txt); 
    diag.show(); 
}Spannable spn=a.getText(); 
    int avalue=Integer.valueOf(spn.toString()); 

這是logcat的:

07-06 14:37:43.063: E/AndroidRuntime(864): FATAL EXCEPTION: main 
    07-06 14:37:43.063: E/AndroidRuntime(864): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.app.LoadedApk.makeApplication(LoadedApk.java:482) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.app.ActivityThread.access$1300(ActivityThread.java:123) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.os.Handler.dispatchMessage(Handler.java:99) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.os.Looper.loop(Looper.java:137) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.app.ActivityThread.main(ActivityThread.java:4424) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at java.lang.reflect.Method.invokeNative(Native Method) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at java.lang.reflect.Method.invoke(Method.java:511) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at dalvik.system.NativeStart.main(Native Method) 
    07-06 14:37:43.063: E/AndroidRuntime(864): Caused by: java.lang.NullPointerException 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.app.LoadedApk.getClassLoader(LoadedApk.java:305) 
    07-06 14:37:43.063: E/AndroidRuntime(864): at android.app.LoadedApk.makeApplication(LoadedApk.java:474) 
    07-06 14:37:43.063: E/AndroidRuntime(864): ... 11 more 
    07-06 14:38:05.103: I/dalvikvm(897): threadid=3: reacting to signal 3 
    07-06 14:38:05.343: I/dalvikvm(897): Wrote stack traces to '/data/anr/traces.txt' 
    07-06 14:38:05.681: I/dalvikvm(897): threadid=3: reacting to signal 3 
    07-06 14:38:05.802: I/dalvikvm(897): Wrote stack traces to '/data/anr/traces.txt' 
    07-06 14:38:05.934: D/dalvikvm(897): GC_FOR_ALLOC freed 60K, 4% free 6524K/6787K, paused 66ms 

這是XML文件格式:

 <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#ffffffff" 
      android:ems="10" 
      android:gravity="center_horizontal" 
      android:inputType="number" 
      android:textColor="#ff000000" > 

      <EditText 
       android:id="@+id/id1" 
       android:inputType="number" /> 

      <EditText 
       android:id="@+id/a1" 
       android:inputType="number" /> 

      <EditText 
       android:id="@+id/f1" 
       android:inputType="number" /> 

      <EditText 
       android:id="@+id/t1" 
       android:inputType="number" /> 

     </TableRow> 
+0

可能是'a is null' – 2012-07-06 10:00:48

+0

這裏'a'是什麼? – 2012-07-06 10:02:45

+0

LoadedApk.java文件中行號爲482的代碼是什麼 – 2012-07-06 10:03:26

回答

1
if (a!=null) { 
     Spannable spn=a.getText(); 
     int avalue=Integer.valueOf(spn.toString()); 
    } 

祝你好運!

+0

它的工作和添加 – dreamhigh 2012-07-06 12:23:53

1
Spannable spn=(Spannable)a.getText(); 
int avalue=Integer.parseInt(spn.toString()); 

UPDATE:

嘗試這樣

int avalue=Integer.parseInt(a.getText().toString()); 
+0

沒有工作。 :( – dreamhigh 2012-07-06 10:26:16

+0

檢查更新.... – 2012-07-06 10:32:17

+0

再次沒有運氣... – dreamhigh 2012-07-06 11:07:33

0

也許「一」是一個TextView或尚未在應用程序中正確地引用又一個視圖,你可以分享「一」是和它已被初始化

+0

我編輯過。檢查它 – dreamhigh 2012-07-06 10:29:23