设置AlarmManager和NumberFormatException?

时间:2012-05-27 16:32:51

标签: android eclipse android-alarms

这是我的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和for {}”,应用程序启动时应用程序可以顺利运行警告消息......任何想法......非常感谢

2 个答案:

答案 0 :(得分: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分配一个值 - 即您尝试使用值“解析Stringdifference“ - 如堆栈跟踪原因所示:

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

答案 1 :(得分:0)

还有另一种将String转换为Long的方法,请尝试以下方式,

Long.valueOf(diffTime[i]);

您可以访问Convert String to Long的好例子。