2012-07-11 100 views
0

我有一个同步逻辑使得从android到db的sendOrder通过在doInBackgroud()上运行的web服务来运行。DoInBackground不能第二次工作

首先我启动应用程序。 然后点击SyncClick和It同步数据库没有错误。 之后,我进入应用程序添加someorder到orderTable 返回到主屏幕(其中有SyncClick),我再次点击SyncClick。 它发送命令(意思是sendOrder()功能正常) 之后,重置数据库并获得最新的数据在doInBackgroud()。 但它得到这个错误;

07-11 23:38:15.406: E/AndroidRuntime(7864): java.lang.RuntimeException: An error occured while executing doInBackground() 

07-11 23:38:15.406: E/AndroidRuntime(7864): Caused by: java.lang.NullPointerException 

后,我开始应用并点击SyncClick再次效果很好。所以它不工作后,我添加了一些命令,然后同步,但没有任何添加任何顺序很好。

寻找问题。 感谢您的时间提前。

public void SyncClick(View v) 
    { 
     Loading = (ImageView)findViewById(R.id.imgLoadingAnim); 
     LoadingLayout = (LinearLayout)findViewById(R.id.loLoadingAnim); 
     LoadingLayout.setVisibility(VISIBLE); 
     Loading.setBackgroundResource(R.drawable.loading); 
     frameAnimation= (AnimationDrawable)Loading.getBackground(); 
     frameAnimation.setCallback(Loading); 
     frameAnimation.setVisible(true, true); 
     frameAnimation.start(); 

     if(!this.dhn.isTableExists("Orders")) 
     { 
      updateDB(); 
     } 

     sendOrder();  
    } 


    public void sendOrder() 
    { 
     ArrayList<Object[]> argumentsList = new ArrayList<Object[]>(); 
     Object[] stuff = {this.dhn, this}; 
     //SEND 
     ArrayList<Order> orders = this.dhn.GetOrders(); 

     for(int i = 0 ; i < orders.size(); i++) 
     { 
      ArrayList<OrderItem> orderItems = this.dhn.GetOrderItems(orders.get(i).ID); 

      String orderItemsS = ""; 

      for(int r = 0 ; r < orderItems.size(); r++) 
      { 
       orderItemsS = orderItemsS + orderItems.get(r).FinalCode + "|" + orderItems.get(r).Quantity + "|" + 
       orderItems.get(r).Price + "|" + orderItems.get(r).Discount + "|" + orderItems.get(r).Status + "|" + "#"; 
      } 

      orderItemsS = orderItemsS.substring(0, orderItemsS.length() - 1); 

      Object[] arguments = { 
        new String("OrderAdd"), 
        stuff, 

        new String("UserId"), 
        new Integer (orders.get(i).UserId), 
        new String("int"), 

        new String("CustomerId"), 
        new Integer (orders.get(i).CustomerId), 
        new String("int"), 

        new String("Price"), 
        new Double (orders.get(i).Price), 
        new String("double"), 

        new String("Discount"), 
        new Double (orders.get(i).Discount), 
        new String("double"), 

        new String("Status"), 
        new Integer (orders.get(i).Status), 
        new String("int"), 

        new String("orderItems"), 
        new String (orderItemsS), 
        new String("String") 
        }; 

      argumentsList.add(arguments); 
     } 

     Object[] stuffALL = {this.dhn, this, argumentsList}; 


     Object[] argumentsALL = { 
       new String("recieveALL"), 
       stuffALL 
       }; 

     //ConnectXML runXMLALL = new ConnectXML(); 
     new ConnectXML().execute(argumentsALL); 


     Status = 1; 

     updateDB(); 

     receive(); 
    } 

    public void receive() 
    { 
     Object[] stuff = {this.dhn, this}; 
     ArrayList<Object[]> argumentsList1 = new ArrayList<Object[]>(); 

     //receive 
     Object[] arguments = { 
       new String("ProductListGet"), 
       stuff, 
       new String("CatID"), 
       new Integer (-1), 
       new String("int") 
       }; 
     argumentsList1.add(arguments); 
     Object[] arguments1 = { 
       new String("CustomerListGet"), 
       stuff 
       }; 
     argumentsList1.add(arguments1); 
     Object[] arguments2 = { 
       new String("CategoryListGet"), 
       stuff, 
       new String("ParentID"), 
       new Integer (-2), 
       new String("int") 
       }; 
     argumentsList1.add(arguments2); 
     Object[] arguments3 = { 
       new String("UserListGet"), 
       stuff 
       }; 
     argumentsList1.add(arguments3); 
     Object[] arguments5 = { 
       new String("ProductCategoriesListGet"), 
       stuff 
       }; 
     argumentsList1.add(arguments5); 
     Object[] arguments6 = { 
       new String("ProductOptionListGet"), 
       stuff 
       }; 
     argumentsList1.add(arguments6); 
     Object[] arguments7 = { 
       new String("FinalProductListGet"), 
       stuff 
       }; 
     argumentsList1.add(arguments7); 
     Object[] arguments8 = { 
       new String("ProductDiscountsListGet"), 
       stuff 
       }; 
     argumentsList1.add(arguments8); 
     Object[] arguments9 = { 
       new String("ProductPriceGroupListGet"), 
       stuff 
       }; 
     argumentsList1.add(arguments9); 
     Object[] arguments10 = { 
       new String("OptionListGet"), 
       stuff 
       }; 
     argumentsList1.add(arguments10); 


     Object[] stuffALL1 = {this.dhn, this, argumentsList1}; 


     Object[] argumentsALL1 = { 
       new String("recieveALL"), 
       stuffALL1 
       }; 

     //ConnectXML runXMLALL1 = new ConnectXML(); 
     new ConnectXML().execute(argumentsALL1); 
    } 

    int Stat; 
    int Status; 
    public void LoadAnim() 
    { 
     if(Status == 1) 
     { 
      Stat++; 
      if(Stat > 0) 
      { 
       LoadingLayout.setVisibility(GONE); 
       if(frameAnimation !=null && frameAnimation.isRunning()) 
       { 
        frameAnimation.stop(); 
        Stat = 0; 
        Status = 0; 
       } 
      } 
     } 
    } 

    public void updateDB() 
    { 
     this.dhn.close(); 

     try { 
      InputStream myInput; 

       myInput = getAssets().open("sistem.db"); 

      // Path to the just created empty db 
      String outFileName = "/data/data/sistem.ss/databases/" 
        + "sistem.db"; 

      // Open the empty db as the output stream 
      OutputStream myOutput = new FileOutputStream(outFileName); 

      // transfer bytes from the inputfile to the outputfile 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) { 
       myOutput.write(buffer, 0, length); 
      } 

      // Close the streams 
      myOutput.flush(); 
      myOutput.close(); 
      myInput.close(); 
      buffer = null; 
      outFileName = null; 
      this.dhn.close(); 
      this.dhn = null; 
      this.dhn = DataHelper.getDataHelper(this); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } 

logcat的

07-12 07:31:59.879: E/AndroidRuntime(2873): FATAL EXCEPTION: AsyncTask #1 
07-12 07:31:59.879: E/AndroidRuntime(2873): java.lang.RuntimeException: An error occured while executing doInBackground() 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at java.lang.Thread.run(Thread.java:1096) 
07-12 07:31:59.879: E/AndroidRuntime(2873): Caused by: java.lang.NullPointerException 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at sistem.ss.ConnectXML.doInBackground(ConnectXML.java:79) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at sistem.ss.ConnectXML.doInBackground(ConnectXML.java:1) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
07-12 07:31:59.879: E/AndroidRuntime(2873):  ... 4 more 
+0

你可以发布整个LogCat输出吗?只是“由......引起:java.lang.NullPointerException”不足以继续。应该有一行指向您的代码中发生空指针的行。 – Eric 2012-07-11 23:21:56

+0

你有没有考虑过使用AsycTask呢? – BlackHatSamurai 2012-07-11 23:22:55

+0

@Soxxeh我加了logcat .. – Mert 2012-07-12 07:10:35

回答

2

我尚未有机会与的AsyncTask工作,但documentation说:

任务只能执行一次(一个例外会如果试图执行第二次执行,则会被抛出。)

这意味着您可以不需要创建一个新的实例并在那个实例上执行。例如。

new WhateverYourTaskIsCalled().execute(...); 

而不是在您已经使用的实例上调用​​。

+0

对于迟到的回复感到抱歉,我只是编辑了一下我的代码,作为您在有疑问的顶部的建议。但仍然给我java.lang.NullPointerException ..我有断点开始的doInBackground()它在断点上跳转,工作看起来很好,但仍然因为异常而暗恋。还尝试创建一个公共任务,并使用第二次,但它说“引起通过:java.lang.IllegalStateException:无法执行任务:该任务已在运行。“感谢你等待你的时间 – Mert 2012-07-12 07:06:45

+0

@Mert听起来你已经在适当的时候使用了一个新的实例,但是你的代码中有一些其他的错误。看看引发'NullPointerException'的确切行,并试图找出你所做的假设是不正确的(该行的某些内容是'null',并且你认为它不应该是)。如果你无法弄清楚,你需要发布相关的代码(你得到Exception的地方和周围环境)。 – 2012-07-12 07:31:58

+0

我在doInBackground()中发现了这个问题..这是一个愚蠢的问题。带走了你们的时间。对不起。我感谢你的时间,所以我接受你的努力的答案。再次感谢。 – Mert 2012-07-12 21:36:25

相关问题