My Intent Service is not starting. It is giving an error. I’m new to android. I’m working in restful web api. I’m consume service on android device. I created a synchronization module in which if there is no covered area app will store data in database and check after every 60 second for network.
When it goes online it will upload data on server. I want this process to run in the background. for this purpose I used Intent Service which runs in background and notify user after execution. But service is not starting I checked many tutorial on net in which they were same method. but mine is not working.
<p><b>Below is my error code</b><p>
<code>
10-11 11:45:30.734: W/dalvikvm(396): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-11 11:45:30.774: E/AndroidRuntime(396): FATAL EXCEPTION: main
10-11 11:45:30.774: E/AndroidRuntime(396): java.lang.RuntimeException: Unable to instantiate service com.remote.synchronizer.haris.OfflineDataService: java.lang.NullPointerException
10-11 11:45:30.774: E/AndroidRuntime(396): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929)
10-11 11:45:30.774: E/AndroidRuntime(396): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
10-11 11:45:30.774: E/AndroidRuntime(396): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
10-11 11:45:30.774: E/AndroidRuntime(396): at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 11:45:30.774: E/AndroidRuntime(396): at android.os.Looper.loop(Looper.java:123)
10-11 11:45:30.774: E/AndroidRuntime(396): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-11 11:45:30.774: E/AndroidRuntime(396): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 11:45:30.774: E/AndroidRuntime(396): at java.lang.reflect.Method.invoke(Method.java:507)
10-11 11:45:30.774: E/AndroidRuntime(396): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-11 11:45:30.774: E/AndroidRuntime(396): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-11 11:45:30.774: E/AndroidRuntime(396): at dalvik.system.NativeStart.main(Native Method)
10-11 11:45:30.774: E/AndroidRuntime(396): Caused by: java.lang.NullPointerException
10-11 11:45:30.774: E/AndroidRuntime(396): at android.content.ContextWrapper.getSystemService(ContextWrapper.java:363)
10-11 11:45:30.774: E/AndroidRuntime(396): at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:742)
10-11 11:45:30.774: E/AndroidRuntime(396): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
10-11 11:45:30.774: E/AndroidRuntime(396): at com.remote.synchronizer.haris.OfflineDataService.<init>(OfflineDataService.java:23)
10-11 11:45:30.774: E/AndroidRuntime(396): at java.lang.Class.newInstanceImpl(Native Method)
10-11 11:45:30.774: E/AndroidRuntime(396): at java.lang.Class.newInstance(Class.java:1409)
10-11 11:45:30.774: E/AndroidRuntime(396): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
10-11 11:45:30.774: E/AndroidRuntime(396): ... 10 more
</code>
<p><b>Intent Service</b></p>
<code><pre>
package com.remote.synchronizer.haris;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.NameValuePair;
import android.app.AlertDialog;
import android.app.IntentService;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class OfflineDataService extends IntentService {
boolean wifi,edge;
private Timer timer= new Timer();
SQLiteDatabase db;
String un,shop,city,date,order,InsertQuery;
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
public OfflineDataService() {
super("OfflineDataService");
}
/*@Override
public void onCreate() {
super.onCreate();
Log.e("Service Example", "Service Started.. ");
db= openOrCreateDatabase("Product", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Order (UserName VARCHAR(10), Shop VARCHAR(15), City VARCHAR(15), Date VARCHAR(10), Order VARCHAR(50));");
} */
@Override
protected void onHandleIntent(Intent intent) {
Bundle bundle=intent.getExtras();
un=bundle.getString("un");
shop=bundle.getString("shop");
city=bundle.getString("city");
date=bundle.getString("date");
order=bundle.getString("order");
/*InsertQuery="INSERT INTO Order VALUES ('"+ un + "','"+ shop + "','" + city + "','" + date + "','" + order + "');";
db.execSQL(InsertQuery);*/
/*timer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
//Checking network connectivity
wifi=NetworkInfo.Wifi(OfflineDataService.this);
edge=NetworkInfo.EDGE(OfflineDataService.this);
if(wifi==true||edge==true)
{
DataSender();
}
else
{
dlgAlert.setMessage("Testing");
dlgAlert.show();
// Toast toast=Toast.makeText(getApplicationContext(), "Testing", toast.LENGTH_LONG).show();
}
}
}, 1000, 5000);*/
}
/*@Override
public void onDestroy() {
super.onDestroy();
Log.e("Service Example", "Service Destroyed.. ");
//notifications
}
private void DataSender()
{
timer.cancel();
}*/
}
</code></pre>
<p><b>Calling Method</b></p>
<code><pre>
Intent serviceIntent= new Intent(this, OfflineDataService.class);
Bundle bundle= new Bundle();
bundle.putString("un", msg);
bundle.putString("shop", shop.getText().toString());
bundle.putString("city", city.getText().toString());
bundle.putString("date", reportDate);
bundle.putString("order", order.getText().toString());
serviceIntent.putExtras(bundle);
startService(serviceIntent);
</code></pre>
<p><b>AndroidManifest Declaration</b></p>
<code><pre>
android:enabled="true"
android:name=".OfflineDataService"
</code></pre>
<p>what is wrong in my code?</p>
<h3><b>Thanks in advance</b></h3>
1/ you cannot use an alertdialog from a service.
2/ even in an Activity, you cannot use it for a Context before onCreate() (that is, you cannot use it in the instanciation process – not in the contstructor, and not in the member declaration)