0

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

4

2 回答 2

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”解析 a - 如堆栈跟踪的原因所示:

Caused by: java.lang.NumberFormatException: Invalid long: "difference"
于 2012-05-27T17:03:54.947 回答
0

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

Long.valueOf(diffTime[i]);

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

于 2012-05-27T17:19:58.103 回答