這是我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 {}」,通過在應用程序啓動時敬酒警告消息,應用程序可以平穩運行......任何想法......非常感謝您
嘗試diffTime [i] .trim(),它可能包含尾隨空格。 – Lucifer
謝謝Lucifer,但它仍然返回相同的錯誤,我認爲Android中有解析var類型時出錯(不僅是Long,而且還有其他類型)... –
okey let'c您能否請求值diffTime數組? – Lucifer