2015-07-20 176 views
1

我目前正試圖顯示當前時間到我的自定義ListView,但我不斷收到一個NullPointerException,並想知道我做錯了什麼,因爲我已經添加到數組並獲取它的值。顯示當前時間

定製適配器和setter/getter方法在這裏:12

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    listView = (ListView) findViewById(R.id.list_view); 
    boss_title = getResources().getStringArray(R.array.boss_array); 
    bossTime = (TextView) findViewById(R.id.boss_time); 

    adapter = new BossAdapter(getApplicationContext(), R.layout.boss_layout); 
    listView.setAdapter(adapter); 
    int i = 0; 


    Thread t = new Thread() { 

     @Override 
     public void run() { 
      try { 
       while (!isInterrupted()) { 
        Thread.sleep(1000); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Calendar c = Calendar.getInstance(); 
          SimpleDateFormat df = new SimpleDateFormat("HH-mm-ss"); 
          String formattedTime = df.format(c.getTime()); 

          String [] boss_time_array = new String[6]; 
          for(int i =0; i < 6; i++){ 
           boss_time_array[i] = formattedTime; 
          } 

          boss_time = boss_time_array; 

         } 
        }); 
       } 
      } catch (InterruptedException e) { 
      } 
     } 
    }; 

    t.start(); 

    for (String boss : boss_title) { 
     Boss bossObject = new Boss(boss_icon[i], boss, boss_time[i]); 

     adapter.add(bossObject); 
     i++; 
    } 

} 

logcat的

8541-8541/baegmon.com.bosstimer E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: baegmon.com.bosstimer, PID: 8541 
java.lang.RuntimeException: Unable to start activity ComponentInfo{baegmon.com.bosstimer/baegmon.com.bosstimer.MainActivity}:  java.lang.NullPointerException: Attempt to read from null array 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5257) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.NullPointerException: Attempt to read from null array 
     at baegmon.com.bosstimer.MainActivity.onCreate(MainActivity.java:67) 
     at android.app.Activity.performCreate(Activity.java:5990) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5257) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

如何修改,以便它不返回空指針錯誤,並返回我的列表視圖的正確時間?

+0

什麼錯誤你好嗎?你能告訴我們你的logcat請 – Soham

+0

@Soham http://paste.ofcode.org/RLPtR8imgbBATjC8V4QFN4 –

+0

請logcat的 – gRaWEty

回答

0
Handler handler=new Handler(); 
handler.post(new Runnable(){ 

     @Override 
     public void run() { 

      // upadte textView here 

      handler.postDelayed(this,500); // set time here to refresh textView 

     } 

    }); 

只是通過你的TextView在hanlder方法。這工作正常。

+0

這似乎已修復null - 指針錯誤,但textview似乎並沒有令人耳目一新? –

+0

你必須設置textview裏面處理程序方法。所以它會在某個時候刷新。 –

0

嘗試從下面的網站實現此代碼。

Calendar c = Calendar.getInstance(); 
SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a"); 
String strDate = sdf.format(c.getTime()); 

希望有任何疑問或疑問,請告訴我。

+0

對不起,我明白如何獲得時間,我問不斷更新文本視圖的時間。 –

0

由於在創建適配器時線程未完成,您將收到NullPointerException。 線程啓動後,您立即讀取boss_title的結果。 這是不可能的,因爲你必須等待它完成。

您應該爲此使用AsyncTask並在onPostExecute(http://developer.android.com/reference/android/os/AsyncTask.html)中創建適配器。

而且SimpleDateFormat的類不是線程安全的:

public static Date dateToString(Date date, final String format, final Locale locale) 
    ThreadLocal<SimpleDateFormat> formater = new ThreadLocal<SimpleDateFormat>() { 
    @Override 
    protected SimpleDateFormat initialValue() { 
     return new SimpleDateFormat(format, locale); 
    } 
    }; 
    return formater.get().format(string); 
} 
+0

啊好吧我可以看到爲什麼這是一個問題,我對AsyncTask如何工作有些困惑,因爲時間會不斷更新,所以技術上線程永遠不會結束,意味着postexecute將不可能? –

+0

然後,您可以使用CountDownTimer,它可以在指定的時間內重複執行(http://developer.android.com/reference/android/os/CountDownTimer.html)。在onTick方法中,您可以檢索新的時間戳 –