2012-05-27 43 views
0

這是我Receiver.java設置AlarmManager和NumberFormatException?

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 

public class Receiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     Toast.makeText(context, intent.getStringExtra("param"),Toast.LENGTH_SHORT).show(); 
    } 

} 

,這是我TaskActivity類..

private void fillData() { 

     // Fields from the database (projection) 
     // Must include the _id column for the adapter to work 
     String[] from = new String[] { TaskTable.COLUMN_SUMMARY }; 
     // Fields on the UI to which we map 
     int[] to = new int[] { R.id.label }; 

     getLoaderManager().initLoader(0, null, this); 

     adapter = new SimpleCursorAdapter(this, R.layout.task_row, null, from,to, 0); 

     setListAdapter(adapter); 
     //String[] from = new String[] { TaskTable.COLUMN_SUMMARY }; 
     String[] diffTime = new String[]{TaskTable.COLUMN_DIFFERENCE}; 
     if(diffTime.length!=0) 
     { 
      AlarmManager alarms = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE); 

      Receiver receiver = new Receiver(); 
      IntentFilter filter = new IntentFilter("ALARM_ACTION"); 
      registerReceiver(receiver, filter); 

      Intent intent = new Intent("ALARM_ACTION"); 
      for(int i=0;i<diffTime.length;i++) 
      { 
       intent.putExtra("param", "WARNING!!! WARNING!!! Task is due now!"); 
       PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0); 

       //alarms.set(AlarmManager.RTC_WAKEUP, d.getTime(), operation); 
       alarms.set(AlarmManager.RTC_WAKEUP, d.getTime()+(Long.parseLong(diffTime[i])), operation); 
       //System.out.println("YEAHHHHHH "+from[i]+" YOOOHOOOO "+(Long.parseLong(diffTime[i]))); 
      } 
     } 

    } 

它返回錯誤這樣的logcat的

05-28 02:27:12.987: E/AndroidRuntime(525): FATAL EXCEPTION: main 
05-28 02:27:12.987: E/AndroidRuntime(525): java.lang.RuntimeException: Unable to start activity ComponentInfo{mad.app/mad.app.TaskActivity}: java.lang.NumberFormatException: Invalid long: "difference" 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.os.Looper.loop(Looper.java:137) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.app.ActivityThread.main(ActivityThread.java:4424) 
05-28 02:27:12.987: E/AndroidRuntime(525): at java.lang.reflect.Method.invokeNative(Native Method) 
05-28 02:27:12.987: E/AndroidRuntime(525): at java.lang.reflect.Method.invoke(Method.java:511) 
05-28 02:27:12.987: E/AndroidRuntime(525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
05-28 02:27:12.987: E/AndroidRuntime(525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
05-28 02:27:12.987: E/AndroidRuntime(525): at dalvik.system.NativeStart.main(Native Method) 
05-28 02:27:12.987: E/AndroidRuntime(525): Caused by: java.lang.NumberFormatException: Invalid long: "difference" 
05-28 02:27:12.987: E/AndroidRuntime(525): at java.lang.Long.invalidLong(Long.java:125) 
05-28 02:27:12.987: E/AndroidRuntime(525): at java.lang.Long.parse(Long.java:362) 
05-28 02:27:12.987: E/AndroidRuntime(525): at java.lang.Long.parseLong(Long.java:353) 
05-28 02:27:12.987: E/AndroidRuntime(525): at java.lang.Long.parseLong(Long.java:319) 
05-28 02:27:12.987: E/AndroidRuntime(525): at mad.app.TaskActivity.fillData(TaskActivity.java:141) 
05-28 02:27:12.987: E/AndroidRuntime(525): at mad.app.TaskActivity.onCreate(TaskActivity.java:48) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.app.Activity.performCreate(Activity.java:4465) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
05-28 02:27:12.987: E/AndroidRuntime(525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
05-28 02:27:12.987: E/AndroidRuntime(525): ... 11 more 

如果我沒有使用「diffTime和for {}」,通過在應用程序啓動時敬酒警告消息,應用程序可以平穩運行......任何想法......非常感謝您

+0

嘗試diffTime [i] .trim(),它可能包含尾隨空格。 – Lucifer

+0

謝謝Lucifer,但它仍然返回相同的錯誤,我認爲Android中有解析var類型時出錯(不僅是Long,而且還有其他類型)... –

+0

okey let'c您能否請求值diffTime數組? – Lucifer

回答

1

你的錯誤:

String[] diffTime = new String[]{TaskTable.COLUMN_DIFFERENCE}; 

TaskTable.COLUMN_DIFFERENCE聲音..很像一個字符串,可命名爲 「差」。

然後你這樣做:

Long.parseLong(diffTime[i]) 

這會導致你的異常。

您是否錯過了您應該從包含「任務表」的數據庫加載內容的步驟?

編輯:

// Here you allocate "diffTime" to, what I must presume is the value "difference". 
    String[] diffTime = new String[]{TaskTable.COLUMN_DIFFERENCE}; 
    if(diffTime.length!=0) 
    { 
     ... 
     ... 
     for(int i=0;i<diffTime.length;i++) 
     { 
      ... 
      ... 
      alarms.set(AlarmManager.RTC_WAKEUP, d.getTime()+(Long.parseLong(diffTime[i])), operation); 
     } 
    } 

你可以從你的代碼中看到,「diffTime」的分配後無處你真的分配比TaskTable.COLUMN_DIFFERENCE以外的值 - 即你正在試圖解析String與值「difference」 - 如由您的堆棧跟蹤的原因:

Caused by: java.lang.NumberFormatException: Invalid long: "difference" 
+0

謝謝Jens,我更新了完整的代碼,如果你想看看我是否錯過了這一步...非常感謝你 –

+0

謝謝Jens,在我調試完畢後應用程序,我發現我錯過了htat步驟從數據庫SQLiteOpenHelper加載...現在我試圖修復代碼...非常感謝你... –

0

還有另一種方式來字符串轉換爲長,嘗試以下方法,

Long.valueOf(diffTime[i]); 

您可以訪問Convert String to Long的一個很好的示例。

+0

謝謝Lucifer,我也嘗試使用那個,以及它仍然有同樣的錯誤:( –

+0

你知道'Long.valueOf(String str)'將調用'parseLong(String str,int radix)',即完全相同的失敗?Long.valueOf(。 )''它會根據實現(等等)有時返回'Long'對象的緩存實例 - 這會減少無意義對象分配的數量。 – Jens