java - 异步任务出错

标签 java android android-asynctask custom-adapter

我在 Android 中收到有关 Asynctask 的错误,我想使用它通过 Web 服务获取数据并将它们绑定(bind)到 GridView

    public class ProductList extends Activity 

{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ``this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_product_list);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            ad=extras.getString("ad");
            catid=extras.getString("catid");
            greeting=(TextView)findViewById(R.id.greetingsText); 
            greeting.setText("Ho�geldiniz, "+extras.getString("ad"));
        }
        Products get = new Products();
        try {
            productsList=get.execute(new String[] { Integer.toString(Integer.parseInt(catid)+1) }).get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < productsList.size(); i++) {
            System.out.println(productsList.get(i).ad);
        }
    }
    public ArrayList<ProductData> productsList = new ArrayList<ProductData>();
    public class ProductData
    {
        public ProductData()
        {

        }

        public ProductData(int id,String ad,String fiyat,String kisaca,String link,String status)
        {
            this.id=id;
            this.ad=ad;
            this.fiyat=fiyat;
            this.kisaca=kisaca;
            this.link=link;
            this.status=status;
        }

        public String status;
        public int id;
        public String ad;
        public String fiyat;
        public String kisaca;
        public String link;
    }
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    }
    private class Products extends AsyncTask<String,  ArrayList<ProductData>, ArrayList<ProductData>> {

        @Override
        protected  ArrayList<ProductData> doInBackground(String... params) {
             URL url;
             ArrayList<ProductData> productsL = new ArrayList<ProductData>();
                try


                {
                     url = new URL("http://www.sucukevim.com/external_services.php?service=get_product_list&&type=filter&&catid="+params[0]);
                URLConnection connection;
                connection = url.openConnection();

                HttpURLConnection httpConnection = (HttpURLConnection)connection;
                int responseCode = httpConnection.getResponseCode();

                if(responseCode==HttpURLConnection.HTTP_OK)
                {
                    InputStream in = httpConnection.getInputStream();

                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();

                    Document dom = db.parse(in);

                    Element docEle = dom.getDocumentElement();

                    NodeList nl = docEle.getElementsByTagName("product"); 
                                    for (int i = 0; i < nl.getLength(); i++) {
                                        Node childNode = nl.item(i);
                                              Element eElement = (Element)childNode;

                                            System.out.println("Baslik= "+getTagValue("id", eElement));
                                            System.out.println("Tarih= "+getTagValue("ad", eElement));
                                            System.out.println("Link= "+getTagValue("resim", eElement));

                                productsL.add(new ProductData(Integer.parseInt(getTagValue("id", eElement)),getTagValue("ad", eElement),getTagValue("fiyat", eElement),getTagValue("kisaca", eElement),getTagValue("resim", eElement),Integer.toString(i)));                 

                            }

                                    return productsL;
                }
                }
                catch(MalformedURLException e)
                {

                }
                catch(IOException e)
                {

                }
                catch(ParserConfigurationException e){

                }
                catch(SAXException e)
                {

                }   
            return null;
        }
        public String getTagValue(String tag,Element eElement)
        {
            NodeList nlList = eElement.getElementsByTagName(tag).item(0).getChildNodes();
            Node nValue = (Node) nlList.item(0);
            return nValue.getTextContent();
        }
        protected void onPostExecute(ArrayList<ProductData> result) {

          }

}
}

Logcat 错误:

05-20 05:23:34.460: E/AndroidRuntime(5406): FATAL EXCEPTION: AsyncTask #3
05-20 05:23:34.460: E/AndroidRuntime(5406): java.lang.RuntimeException: An error occured while executing doInBackground()
05-20 05:23:34.460: E/AndroidRuntime(5406):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at java.lang.Thread.run(Thread.java:1019)
05-20 05:23:34.460: E/AndroidRuntime(5406): Caused by: java.lang.NullPointerException
05-20 05:23:34.460: E/AndroidRuntime(5406):     at com.Troyateck.sucukevim.ProductList$Products.getTagValue(ProductList.java:164)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at com.Troyateck.sucukevim.ProductList$Products.doInBackground(ProductList.java:136)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at com.Troyateck.sucukevim.ProductList$Products.doInBackground(ProductList.java:1)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-20 05:23:34.460: E/AndroidRuntime(5406):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-20 05:23:34.460: E/AndroidRuntime(5406):     ... 4 more

最佳答案

问题出在 getTagValue() 上:

public String getTagValue(String tag,Element eElement)
{
    NodeList nlList = eElement.getElementsByTagName(tag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0);
    return nValue.getTextContent();
}

第 164 行某处有一个空指针。我不确定具体是哪个表达式,但是这三行中的任何一行都可能导致空指针异常。使用前请检查每个返回值是否为 null,也许像这样:

public String getTagValue(String tag,Element eElement)
{
    String retVal = "NOT FOUND";
    if (eElement != null && eElement.getElementsByTagName(tag) != null && eElement.getElementsByTagName(tag).item(0) != null)
    {
        NodeList nlList = eElement.getElementsByTagName(tag).item(0).getChildNodes();
        if (nlList != null && nlList.item(0) != null)
        {
            Node nValue = (Node) nlList.item(0);
            retVal = nValue.getTextContent();
        }
    }

    return retVal;
}

关于java - 异步任务出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16642020/

相关文章:

java - 我可以为 Android 制作 HTML5 游戏吗?

java - 将 jar 作为 Linux 服务运行 - init.d 脚本在启动应用程序时卡住

java - 当字符串输入参数的第一个字符不是预期的字符时抛出什么异常?

java - 在Android中创建自定义按钮类

Android 应用内计费和存储托管购买

Android AsyncTask - 执行顺序

java - 如果加载超过一定时间则关闭 AsyncTask(android)

java - JAXB 2 Maven 插件 - 强制生成 java 源代码,以使用子集合?

java - org.openqa.selenium.SessionNotCreatedException : A new session could not be created.(原始错误: 'java -version' failed.错误:spawn ENOENT)

Android在PreferenceActivity中添加AsyncTask 第一次加载大量preferences