-1

I used a service for syncing process in my android app. Now i want to add a progress bar to show that syncing process is going on. But when start the service app stops showing following error. But i don't know why it's happen,

Error,

10-23 05:27:56.390: E/AndroidRuntime(25458): FATAL EXCEPTION: Timer-0
10-23 05:27:56.390: E/AndroidRuntime(25458): Process: com.abc, PID: 25458
10-23 05:27:56.390: E/AndroidRuntime(25458): java.lang.NullPointerException
10-23 05:27:56.390: E/AndroidRuntime(25458):    at com.abc.service.SynchingService$1.run(SynchingService.java:51)
10-23 05:27:56.390: E/AndroidRuntime(25458):    at java.util.Timer$TimerImpl.run(Timer.java:284)

this is my code for service,

public class SynchingService extends Service {

private Timer t;
private static MainActivity mainActivity;
ProgressDialog dialog;

@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    this.timer();
    return Service.START_STICKY;
}

private void timer() {
    t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {

            mainActivity.runOnUiThread(new Runnable() {
                public void run() {
                    dialog = ProgressDialog.show(mainActivity, getString(R.string.dialog_saving), getString(R.string.dialog_please_wait));
                }
            });
            callSync();

        }
    }, 1000, 1000 * 60 * 60);

}

/**
 * This method is used to call every table to start syncing
 * 
 * @param Nothing
 * @return No return value
 */
private void callSync() {

    DataManager datamanager = DataManager.getInstance(getApplicationContext());

    Log.d("service", "running");
    // sync contact data
    ContactSync contactSync = new ContactSync();
    contactSync.contactGroupSync();
    contactSync.contactSync();

    Log.d("service2", "running2");
    // sync horse data
    HorseSync horseSync = new HorseSync();
    horseSync.horseSyncWithServer();

    Log.d("service3", "running3");
    // sync blog data
    BlogSync blogSync = new BlogSync();
    blogSync.blogSyncWithServer();

    Log.d("service4", "running4");

    // delete news categories from local DB.
    datamanager.deleteNewsCategories(MainActivity.mainActivity);
    // delete news from local DB.
    datamanager.deleteNews(MainActivity.mainActivity);
    // delete advertisement categories from local db.
    datamanager.deleteAddCategories(MainActivity.mainActivity);
    // delete advertisements from local db.
    datamanager.deleteAdds(MainActivity.mainActivity);

    dialog.dismiss();

}
@Override
public void onDestroy() {
    super.onDestroy();
}

}

please help me to fix this issue.

user3800832
  • 421
  • 2
  • 8
  • 22

3 Answers3

0

because in your case mainActivity is null. you didn't initialized it.

Rustam
  • 6,485
  • 1
  • 25
  • 25
  • mainActivity.runOnUiThread will also cause a crash, and you cannot replace the activity reference by getApplicationContext() since runOnUiThread is not a method of Context... – 2Dee Oct 23 '14 at 09:54
  • it would be ok if they will create a `public satatic Acitivty mainActivity` in `MainActivity` and initialize there. and here they can access. – Rustam Oct 23 '14 at 10:00
0

You forgot to initialize your mainActivity variable, so this line will cause a crash :

mainActivity.runOnUiThread

It's generally not a good idea to show a Dialog from a Service, read this question and its accepted answer for details. You can look at this from a UX point of view : if you use the Service to sync data whether your app is open or not, that means you want to show a Dialog (thus blocking UI) that your app is syncing when the user might be performing a totally different task on his device, not related to your app, and I think we can agree that's bad. Or you use the Service to sync data only when your app is open, in which case you could just use an AsyncTask.

Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
0

I think you should add new class to your project for getting context

So you can do it like :

Add new class and extend it from Application

public class MyApplication extends Application{

    private static Context context;

    public void onCreate(){
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}

and Declare it at manifest:

<application android:name="com.xyz.MyApplication">
</application>

Finally you can use that class when will you need context :)

gokhan
  • 627
  • 1
  • 6
  • 16
  • This will not fix the issue of the crash caused by mainActivity.runOnUiThread, you cannot call runOnUiThread on Context. – 2Dee Oct 23 '14 at 09:56
  • Ok, Yes @2Dee you right,Another Solution is using Messenger for communication with ui thread,Because service is viewless – gokhan Oct 23 '14 at 10:02
  • [link](http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging) will provide good example – gokhan Oct 23 '14 at 10:06