无法在未调用Looper.prepare()android服务的线程内创建处理程序

时间:2015-05-24 05:43:33

标签: java android multithreading service

嗨,我有一个问题,我没有解决问题 问题定义şs无法在未调用Looper.prepare()的线程内创建处理程序。我在android中编写服务,但我得到抛出异常 我的Logcat就是那个

05-24 01:37:37.069  26269-26292/ramazan.com.dovizfiyarlari E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: ramazan.com.dovizfiyarlari, PID: 26269
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:200)
            at android.os.Handler.<init>(Handler.java:114)
            at android.app.Activity.<init>(Activity.java:765)
            at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:75)
            at android.support.v7.app.ActionBarActivity.<init>(ActionBarActivity.java:59)
            at ramazan.com.dovizfiyarlari.tab1.<init>(tab1.java:43)
            at ramazan.com.dovizfiyarlari.tab1$Dolar.doInBackground(tab1.java:180)
            at ramazan.com.dovizfiyarlari.tab1$Dolar.doInBackground(tab1.java:150)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

我的服务就是那个

 public static 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) {

        }
        public void onCreate()
        {
            super.onCreate();
            ctx = this;
            startService();
        }

        private void startService()
        {

            timer.scheduleAtFixedRate(new mainTask(), 0, 500000);
        }

        private class mainTask extends TimerTask
        {
            private Handler mHandler = new Handler(Looper.getMainLooper());

            @Override
            public void run() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                     new Dolar().execute();
                    }
                });
            }
        }

        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)
            {

            }
        };
    }

正在运行代码

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab1);
        init();
        if(isNetworkAvailable(tab1.this)==true) {
            startService(new Intent(tab1.this,LocalService.class));
            btn_dolar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), Para.class);
                    sonuc = Double.parseDouble(txt_dolar.getText().toString()) * Double.parseDouble(txt_girilecek_dolar.getText().toString());
                    startActivity(intent);
                }
            });
        }
        else
        {
            Toast.makeText(getApplicationContext(),"internet baglantınız yoktur",Toast.LENGTH_LONG).show();
        }
    }
    
    
    ----------------------------------------------------
      
      
    public static class Dolar extends AsyncTask<String,String,String>{
    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        tab1 ta=new tab1();
        ta.txt_dolar.setText(ta.kur);
        ta.txt_altin_gram.setText(altin);
      ta.  txt_ceyrek.setText(ceyrek);

    }
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();
        }
        tab1 tab=new tab1();
        int sayac = 0;
        Elements sondakika = document.select("div.norm");
        for (Element element : sondakika) {
             if(sayac==0){
                 tab.kur=element.text();
             }
            sayac=sayac+1;
        }

我该怎么办?你能帮我吗......

祝福

1 个答案:

答案 0 :(得分:2)

您似乎在后台线程中使用了AsyncTask。您无法从后台线程执行AsyncTask。因为,您已经在后台线程中,所以您不需要AsyncTask。只需直接执行您的代码。