在Thread中没有调用Run方法

时间:2016-04-11 02:10:52

标签: java android xml multithreading

我正在尝试使用单独的线程解析xml。整个代码的小代码片段如下:

Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
           try{
            c=c1;
            URL url = new URL(url1);
            HttpURLConnection con= (HttpURLConnection) url.openConnection();
            InputStream i= con.getInputStream();
            xf=XmlPullParserFactory.newInstance();
            xp=xf.newPullParser();
            xp.setInput(i,null);
            parseXML(xp,c);
        }
        catch(Exception e)
        {
            e.printStackTrace();

        }


        }
    });
    t.start();

但似乎run方法本身并未被调用。有人可以帮忙找出我在这里缺少的东西。

编辑: 以下是与XML相关的整个代码:

public void fetchXml(String u,final Context c1) throws IOException,   XmlPullParserException, InterruptedException {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
           try{

            c=c1;
               URL url = new URL(url1);
            HttpURLConnection con= (HttpURLConnection) url.openConnection();
            InputStream i= con.getInputStream();
            xf=XmlPullParserFactory.newInstance();
            xp=xf.newPullParser();
            xp.setInput(i,null);
            parseXML(xp,c);
        }
        catch(Exception e)
        {

            e.printStackTrace();

        }
     }
    });

    t.start();
    t.join();
   }


  public void parseXML(XmlPullParser xp,Context c) throws XmlPullParserException, IOException {

    int event;
    String text=null;
    Toast.makeText(c, "inside parse", Toast.LENGTH_LONG).show();

    event=xp.getEventType();

    while(event!=XmlPullParser.END_DOCUMENT)
    {

        String name=xp.getName();

        switch(event)
        {

            case XmlPullParser.START_TAG :
                break;

            case XmlPullParser.TEXT :
                text=xp.getText();
                break;

            case XmlPullParser.END_TAG :
                if(name.equals("country")){
                    country=text;
                    Toast.makeText(c, "Country"+country, Toast.LENGTH_LONG).show();
                }
                else if(name.equals("humidity")){

                    humidity=xp.getAttributeValue(null,"value");
                    Toast.makeText(c, "humidity"+humidity,  Toast.LENGTH_LONG).show();
                }

                else if (name.equals("pressure"))
                {
                    pressure=xp.getAttributeValue(null,"value");
                    Toast.makeText(c, "pressure"+pressure,  Toast.LENGTH_LONG).show();
                }
                else{}
                break;
   }
        event=xp.next();
  }
    parsingcomplete=true;
  }

当我检查logcat时,我在上面的parsexml函数中写的Toast语句出现以下错误。

 04-11 07:59:08.891 31361-31580/com.example.hp.xmlparsing W/System.err:   java.lang.RuntimeException: Can't create handler inside thread that has not  called Looper.prepare()
 04-11 07:59:08.892 31361-31580/com.example.hp.xmlparsing W/System.err:     at android.os.Handler.<init>(Handler.java:200)

看起来run方法正常运行..但我得到了例外:

 java.lang.RuntimeException: Can't create handler inside thread that has not    called Looper.prepare()

有人可以帮忙......

0 个答案:

没有答案