嗨有一个问题,并没有找到解决方案我写的android服务和该异常得到“java.lang.InstantiationException” 我的logcat就是那个
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;
}
}
请你帮帮我...
答案 0 :(得分:1)
删除此行,它没用,是问题的原因。你无法在Android中手动实例化服务/活动,它禁止它,因此你的例外。
LocalService localService = new LocalService();