0

This is my 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();
    }

}

and this is my TaskActivity class..

    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])));
            }
        }

    }

It returns error like this in the 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

If I'm not using "diffTime and for{}", the application run smoothly by toasting the Warning message at the time the application starts... any ideas... thank you very much

Romans 8.38-39
  • 456
  • 4
  • 14
  • try diffTime[i].trim() , it might contain trailing space. – Lucifer May 27 '12 at 16:43
  • Thanks Lucifer, but it's still return the same error, I think there is something wrong in Android with parsing var type(not only Long, but the other type as well)... – Romans 8.38-39 May 27 '12 at 16:48
  • okey let'c can you please the values of diffTime array ? – Lucifer May 27 '12 at 16:50
  • In my place, now is 3 am and I've just inputted the due date is today and the due time is 4 am (so the difference time will be 1 hour only) This is the logCat to make it clear: this is today : 28 4 112 3 1 (28-04-2012 03:01AM) this is taskDueTime : 61296282000000 this is Different time : 59958143923850 The DiffTime[] will contain Different Time with String type... – Romans 8.38-39 May 27 '12 at 17:04
  • From your logcat i can only retrieve that you are trying to convert an integer which is in string format to long format, which is wrong, try once Integer.parseInt() – Lucifer May 27 '12 at 17:11
  • He's trying to convert a String with the value "difference" to a Long. Converting it to an Integer would ofc also fail. – Jens May 27 '12 at 17:14
  • Yes, I'm trying to convert String to Long because the date.getTime() will be in long format... – Romans 8.38-39 May 27 '12 at 17:30

2 Answers2

1

Your error:

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

TaskTable.COLUMN_DIFFERENCE sounds .. very much like a string, that could be named "difference".

Then you do this:

Long.parseLong(diffTime[i])

which causes your exception.

Did you miss a step where you were supposed to load stuff from a database containing a "task table"?

Edit:

// 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);
        }
    }

As you can see from your code, nowhere after the allocation of "diffTime" do you actually assign it a value other than TaskTable.COLUMN_DIFFERENCE - i.e. you are attempting to parse a String with the value "difference" - as shown by the cause of your stack trace:

Caused by: java.lang.NumberFormatException: Invalid long: "difference"
Jens
  • 16,853
  • 4
  • 55
  • 52
  • Thanks Jens,I've updated the full code, if you want to see whether I miss the step or not... Thank you very much – Romans 8.38-39 May 27 '12 at 17:29
  • Thanks Jens, after I debug the application, I found that I missed htat step to load from db SQLiteOpenHelper... Now I'm trying to fix the code... Thank you very much... – Romans 8.38-39 May 27 '12 at 17:47
0

There is another way to convert String to Long, try following way,

Long.valueOf(diffTime[i]);

You can visit a good example of Convert String to Long.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • Thanks Lucifer, I've trying using that one too, and it still got same error :( – Romans 8.38-39 May 27 '12 at 17:28
  • You are aware that `Long.valueOf(String str)` will call `parseLong(String str, int radix)`, i.e. exactly the same failure? The primary diff for `Long.valueOf(..)` is that it will, depending on the implementation (etc. etc.) sometimes return a cached instance of the `Long` object - which reduces the number of pointless object allocations. – Jens May 27 '12 at 17:28