gpt4 book ai didi

android - java.lang.InstantiationException 安卓

转载 作者:太空狗 更新时间:2023-10-29 14:09:09 24 4
gpt4 key购买 nike

你好,我有一个问题,但没有找到解决方法我的逻辑是

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
worker 代码是

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

}

你能帮帮我吗...

最佳答案

删除这一行,它没有用,也是你问题的原因。您不能在 Android 中手动实例化服务/Activity ,它禁止它,因此您的异常(exception)。

 LocalService localService = new LocalService();

关于android - java.lang.InstantiationException 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com