2014-03-18 32 views
0

我正在嘗試編寫一個程序,它將在EditText字段中輸入數字並將其轉換爲整數,以便稍後可以使用它爲應用程序設置一個計時器。我想這樣做,如果點擊2個按鈕中的一個,EditText將被檢查。如果在那裏沒有輸入任何內容,我想要彈出一條消息,要求輸入計時器的秒數。如果有一個數字,我希望程序開始一個新的活動。這是我的代碼:NullPointerException字符串爲int

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.ComponentName; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class Start extends Activity { 
    Button button1; 
    EditText et; 
    String timer; 
    int timer1; 
    Button button2; 
    public void onCreate (Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.start); 
     button1 = (Button)findViewById(R.id.button1); 
     button2 = (Button)findViewById(R.id.button2); 
     et = (EditText)findViewById(R.id.editText1); 


     button2.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       if (timer.isEmpty()){ 
        AlertDialog.Builder build = new AlertDialog.Builder (Start.this); 
        build.setMessage("Please, type in the number of seconds"); 
        build.setNegativeButton("Okay,Okay", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
          finish(); 
         } 
        }); 
        AlertDialog alert = build.create(); 
        alert.show(); 

       } 
       else{ 
        bull(); 

       Intent startMain = new Intent(Intent.ACTION_MAIN); 
       startMain.addCategory(Intent.CATEGORY_HOME); 
       startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(startMain); 
       timer = et.getText().toString(); 
       bull(); 
       int secondsDelayed = 1; 
       new Handler().postDelayed(new Runnable() { 
         public void run() { 
          Intent intent = new Intent(Start.this, MainActivity.class); 

          startActivity(intent); 
         } 
       }, secondsDelayed * timer1*1000); 
      } 

     } 
     }); 

     button1.setOnClickListener (new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     if (timer.isEmpty()){ 
      AlertDialog.Builder build = new AlertDialog.Builder (Start.this); 
      build.setMessage("Please, type in the number of seconds"); 
      build.setNegativeButton("Okay,Okay", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        finish(); 
       } 
      }); 
      AlertDialog alert = build.create(); 
      alert.show(); 

     } 
     else{ 
      bull(); 

     Intent startMain = new Intent(Intent.ACTION_MAIN); 
     startMain.addCategory(Intent.CATEGORY_HOME); 
     startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(startMain); 
     bull(); 
     int secondsDelayed = 1; 
     new Handler().postDelayed(new Runnable() { 
       public void run() { 
        Intent intent = new Intent(Start.this, MainActivity2.class); 

        startActivity(intent); 
       } 
     }, secondsDelayed * timer1*1000); 
    } 
    } 
}); 

    } 

    private void bull() { 
     // TODO Auto-generated method stub 



     timer1 = Integer.parseInt(timer); 

    } 



} 

但是,我得到NullPointerException。我的錯誤在哪裏?由於

這裏是我的logcat:

03-18 22:16:57.506: I/Process(3008): Sending signal. PID: 3008 SIG: 9 
03-18 22:17:19.948: D/skia(3858): new locale en-Latn-GB 
03-18 22:17:19.998: D/dalvikvm(3858): GC_FOR_ALLOC freed 80K, 7% free 17272K/18412K, paused 16ms, total 16ms 
03-18 22:17:20.008: I/dalvikvm-heap(3858): Grow heap (frag case) to 20.149MB for 2359312-byte allocation 
03-18 22:17:20.018: D/dalvikvm(3858): GC_FOR_ALLOC freed 1K, 6% free 19574K/20720K, paused 14ms, total 14ms 
03-18 22:17:20.038: D/dalvikvm(3858): GC_CONCURRENT freed <1K, 6% free 19574K/20720K, paused 3ms+1ms, total 18ms 
03-18 22:17:20.148: D/libEGL(3858): loaded /system/lib/egl/libEGL_adreno200.so 
03-18 22:17:20.148: D/libEGL(3858): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
03-18 22:17:20.148: D/libEGL(3858): loaded /system/lib/egl/libGLESv2_adreno200.so 
03-18 22:17:20.148: I/Adreno200-EGL(3858): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: (CL3579225) 
03-18 22:17:20.148: I/Adreno200-EGL(3858): Build Date: 04/22/13 Mon 
03-18 22:17:20.148: I/Adreno200-EGL(3858): Local Branch: adreno_sba_20130419 
03-18 22:17:20.148: I/Adreno200-EGL(3858): Remote Branch: 
03-18 22:17:20.148: I/Adreno200-EGL(3858): Local Patches: 
03-18 22:17:20.148: I/Adreno200-EGL(3858): Reconstruct Branch: 
03-18 22:17:20.208: D/OpenGLRenderer(3858): Enabling debug mode 0 
03-18 22:17:28.096: D/GestureDetector(3858): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 11 mFalseSizeCnt:0 
03-18 22:17:29.167: D/GestureDetector(3858): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0 
03-18 22:17:29.187: D/AndroidRuntime(3858): Shutting down VM 
03-18 22:17:29.187: W/dalvikvm(3858): threadid=1: thread exiting with uncaught exception (group=0x4104fac8) 
03-18 22:17:29.187: E/AndroidRuntime(3858): FATAL EXCEPTION: main 
03-18 22:17:29.187: E/AndroidRuntime(3858): java.lang.NullPointerException 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at com.example.scaryme.Start$2.onClick(Start.java:78) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at android.view.View.performClick(View.java:4354) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at android.view.View$PerformClick.run(View.java:17962) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at android.os.Handler.handleCallback(Handler.java:725) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at android.os.Looper.loop(Looper.java:137) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at android.app.ActivityThread.main(ActivityThread.java:5328) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
03-18 22:17:29.187: E/AndroidRuntime(3858):  at dalvik.system.NativeStart.main(Native Method) 
+1

什麼是78行'Start.java'? – Raghunandan

回答

0

您的問題似乎是,你永遠不會初始化計時器。因此,在點擊方法中調用timer.isEmpty()失敗,因爲timer爲空。你應該能夠通過給定時器默認值「」來解決這個問題。

+0

'timer = et.getText()。toString();' – Raghunandan

+0

直到timer.isEmpty()之後纔會調用它。 –

+0

可能會,但這是一個猜測。 – Raghunandan