0

我在 android 模拟器上收到此错误。我正在使用它从用户那里获取 int 值:它适用于 String 值,但不适用于 int 值。也尝试了 parseInt() 但没有运气。

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

这是日志:

   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>
4

2 回答 2

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

更新:

像这样试试

int avalue=Integer.parseInt(a.getText().toString());
于 2012-07-06T10:04:21.473 回答
0
  if ( a!=null ) {
      Spannable spn=a.getText();
      int avalue=Integer.valueOf(spn.toString());
  }

祝你好运!

于 2012-07-06T10:08:47.237 回答