hi ı have a problem and did not find resolve i write android service and that exception get "java.lang.InstantiationException" My logcat is that
05-23 09:44:20.164 10374-10374/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service ramazan.com.dovizfiyarlari.tab1$LocalService: java.lang.InstantiationException: can't instantiate class ramazan.com.dovizfiyarlari.tab1$LocalService; no empty constructor
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2347)
at android.app.ActivityThread.access$1600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class ramazan.com.dovizfiyarlari.tab1$LocalService; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2344)
at android.app.ActivityThread.access$1600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
public class LocalService extends IntentService
{
public LocalService () {
super("MyServerOrWhatever");
}
private Timer timer = new Timer();
private Context ctx;
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
Dolar dolar = new Dolar();
dolar.execute();
}
public void onCreate()
{
super.onCreate();
ctx = this;
startService();
}
private void startService()
{
timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
}
private class mainTask extends TimerTask
{
public void run()
{
toastHandler.sendEmptyMessage(0);
}
}
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}
private final Handler toastHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
Dolar dolar=new Dolar();
dolar.execute();
}
};
}
and
if(isNetworkAvailable(tab1.this)==true) {
LocalService localService=new LocalService();
startService(new Intent(tab1.this,LocalService.class));
]
public class Dolar extends AsyncTask<String,String,String>{
ProgressDialog pd;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(tab1.this);
pd.setMessage("yukleniyor");
pd.show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
txt_dolar.setText(kur);
txt_altin_gram.setText(altin);
txt_ceyrek.setText(ceyrek);
pd.dismiss();
}
String altin="";
String ceyrek;
@Override
protected String doInBackground(String... params) {
Document document = null;
try {
document = Jsoup.connect("http://www.reuters.com/finance/currencies/quote?srcAmt=1.00&srcCurr=USD&destAmt=&destCurr=TRY")
.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.1.25 (KHTML, like Gecko) " +
"Version/8.0 Safari/600.1.25").get();
} catch (IOException e) {
e.printStackTrace();
}
int sayac = 0;
Elements sondakika = document.select("div.norm");
for (Element element : sondakika) {
if(sayac==0){
kur=element.text();
}
sayac=sayac+1;
}
}
can your help me please...
Remove this line, it's useless and the cause of your problem. You can't manually instantiate Services/Activities in Android, it forbids it and hence your exception.
LocalService localService = new LocalService();