这是我的 Receiver.java
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 and for{}”,应用程序会在应用程序启动时通过发出警告消息顺利运行...任何想法...非常感谢