DoInBackground不起作用第二次 [英] DoInBackground doesn't work second time
问题描述
我有一个同步逻辑使得sendOrder从数据库在android系统服务器通过运行在 Web服务doInBackgroud()
。
首先,我开始应用。
然后点击 SyncClick
和它没有错误同步数据库。
之后我进入应用someorder添加到orderTable
返回到主屏幕(其中有SyncClick),我再次点击SyncClick。
它发送顺序(即 sendOrder()
功能效果很好)
后复位数据库并获得最新数据doInBackgroud()
。
但得到这个错误;
07-11 23:38:15.406:E / AndroidRuntime(7864):了java.lang.RuntimeException:执行doInBackground发生错误()07-11 23:38:15.406:E / AndroidRuntime(7864):因:显示java.lang.NullPointerException
后,我开始应用,然后点击SyncClick再次效果很好。所以我添加一些订单,然后同步,但效果很好,没有任何顺序添加后不能正常工作。
找问题。
感谢您的时间提前。
公共无效SyncClick(视图V)
{
装载=(ImageView的)findViewById(R.id.imgLoadingAnim);
LoadingLayout =(的LinearLayout)findViewById(R.id.loLoadingAnim);
LoadingLayout.setVisibility(可见);
Loading.setBackgroundResource(R.drawable.loading);
frameAnimation =(AnimationDrawable)Loading.getBackground();
frameAnimation.setCallback(装载);
frameAnimation.setVisible(真实,真实);
frameAnimation.start(); 如果(!this.dhn.isTableExists(订单))
{
数据库更新();
} sendOrder();
}
公共无效sendOrder()
{
ArrayList的<对象[]> argumentsList =新的ArrayList<对象[]>();
[对象]东西= {this.dhn,这};
//发送
ArrayList的<排序>订单= this.dhn.GetOrders(); 的for(int i = 0; I< orders.size();我++)
{
ArrayList的<&OrderItem的GT;的OrderItems = this.dhn.GetOrderItems(orders.get(我).ID); 串orderItemsS =; 对于(INT R = 0;为r orderItems.size(); R ++)
{
orderItemsS = orderItemsS + orderItems.get(R).Final code +| + orderItems.get(R).Quantity +| +
orderItems.get(R)。价格+| + orderItems.get(R).Discount +| + orderItems.get(R)+ .STATUS| +#;
} orderItemsS = orderItemsS.substring(0,orderItemsS.length() - 1); [对象]参数= {
新的String(OrderAdd),
东东, 新的String(用户ID),
新的整数(orders.get(我).UserId)
新的String(INT), 新的String(客户编号),
新的整数(orders.get(我).CustomerId)
新的String(INT), 新的String(价格),
新的双(orders.get(我)。价格)
新的String(双), 新的String(打折),
新的双(orders.get(我).Discount)
新的String(双), 新的String(状态),
新的整数(orders.get(我).STATUS)
新的String(INT), 新的String(的OrderItems),
新的String(orderItemsS)
新的String(字符串)
}; argumentsList.add(参数);
} [对象] stuffALL = {this.dhn,对此,argumentsList};
[对象] argumentsALL = {
新的String(recieveALL),
stuffALL
}; // ConnectXML runXMLALL =新ConnectXML();
新ConnectXML()执行(argumentsALL);
状态= 1; 数据库更新(); 接收();
} 公共无效接收()
{
[对象]东西= {this.dhn,这};
ArrayList的<对象[]> argumentsList1 =新的ArrayList<对象[]>(); //接收
[对象]参数= {
新的String(ProductListGet),
东东,
新的String(CATID),
新的整数(-1),
新的String(INT)
};
argumentsList1.add(参数);
[对象] arguments1 = {
新的String(CustomerListGet),
东东
};
argumentsList1.add(arguments1);
[对象] arguments2 = {
新的String(CategoryListGet),
东东,
新的String(PARENTID),
新的整数(-2),
新的String(INT)
};
argumentsList1.add(arguments2);
[对象] arguments3 = {
新的String(UserListGet),
东东
};
argumentsList1.add(arguments3);
[对象] arguments5 = {
新的String(ProductCategoriesListGet),
东东
};
argumentsList1.add(arguments5);
[对象] arguments6 = {
新的String(ProductOptionListGet),
东东
};
argumentsList1.add(arguments6);
[对象] arguments7 = {
新的String(FinalProductListGet),
东东
};
argumentsList1.add(arguments7);
[对象] arguments8 = {
新的String(ProductDiscountsListGet),
东东
};
argumentsList1.add(arguments8);
[对象] arguments9 = {
新的String(ProductPriceGroupListGet),
东东
};
argumentsList1.add(arguments9);
[对象] arguments10 = {
新的String(OptionListGet),
东东
};
argumentsList1.add(arguments10);
[对象] stuffALL1 = {this.dhn,对此,argumentsList1};
[对象] argumentsALL1 = {
新的String(recieveALL),
stuffALL1
}; // ConnectXML runXMLALL1 =新ConnectXML();
新ConnectXML()执行(argumentsALL1);
} INT统计;
INT状态;
公共无效LoadAnim()
{
如果(状态== 1)
{
统计++;
如果(统计大于0)
{
LoadingLayout.setVisibility(GONE);
如果(frameAnimation = NULL&放大器;!&安培; frameAnimation.isRunning())
{
frameAnimation.stop();
STAT = 0;
状态= 0;
}
}
}
} 公共无效数据库更新()
{
this.dhn.close(); 尝试{
InputStream的myInput; myInput = getAssets()开(sistem.db); //路径刚刚创建的空分贝
字符串outFileName =/data/data/sistem.ss/databases/
+sistem.db //打开空分贝的输出流
的OutputStream myOutput =新的FileOutputStream(outFileName); //传递从inputfile中字节到OUTPUTFILE
字节[]缓冲区=新的字节[1024];
INT长;
而((长度= myInput.read(缓冲液))大于0){
myOutput.write(缓冲液,0,长度);
} //关闭流
myOutput.flush();
myOutput.close();
myInput.close();
缓冲= NULL;
outFileName = NULL;
this.dhn.close();
this.dhn = NULL;
this.dhn = DataHelper.getDataHelper(本);
}赶上(IOException异常E1){
// TODO自动生成catch块
e1.printStackTrace();
}
}
LogCat中
七月七日至12日:31:59.879:E / AndroidRuntime(2873):致命异常:AsyncTask的#1
七月七日至12日:31:59.879:E / AndroidRuntime(2873):了java.lang.RuntimeException:执行doInBackground发生错误()
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:561)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在java.lang.Thread.run(Thread.java:1096)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):因:显示java.lang.NullPointerException
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在sistem.ss.ConnectXML.doInBackground(ConnectXML.java:79)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在sistem.ss.ConnectXML.doInBackground(ConnectXML.java:1)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
七月七日至12日:31:59.879:E / AndroidRuntime(2873):... 4个
我尚未有机会与AsyncTask的工作,但是的文档说:
该任务可以只执行一次(如果第二次尝试执行一个异常将被抛出。)
块引用>这意味着你只需要创建一个新的实例并执行一个。例如。
新WhateverYourTaskIsCalled()执行(......)。
而不是调用
的execute()
您已经使用的实例。I have a synchronism logic makes sendOrder from db in android to server by web service which runs on
doInBackgroud()
.first I start app. then click
SyncClick
and It Sync database without error. after that I go into application add someorder to orderTable return to main screen(which has SyncClick) and I click SyncClick again. It sends order (which meanssendOrder()
function works well) after that reset database and get latest data ondoInBackgroud()
. but It getting this error;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
after I start app and click SyncClick works well again. so It doesn't work after I add some order then Sync but works well without any order added.
looking for problem. thanks for your time in advance.
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
解决方案I haven't yet had a chance to work with AsyncTask, but the documentation says:
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
That means you just need to create a new instance and execute on that one. E.g.
new WhateverYourTaskIsCalled().execute(...);
Rather than calling
execute()
on the instance you've already used.这篇关于DoInBackground不起作用第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!