不适用于活动的onCreate()之前系统服务 [英] System services not available to Activities before onCreate()
问题描述
我在的onCreate得到这个不适用于活动的异常系统服务()时,我试图设置适配器我的
方法。的onCreate之外
()
结果我已经使用 runOnUIThread()
函数试过,但还是没能解决我的问题。结果这是我的code其实。结果我m蒙特的方法从我的服务子发送列表,我的活动课。
类KitchenSerivce延伸服务
@覆盖
公众诠释onStartCommand(意向意图,诠释旗帜,INT startId)
{
super.onStartCommand(意向,旗帜,startId);
Toast.makeText(这一点,服务启动...,Toast.LENGTH_SHORT).show(); 尝试
{
SoapObject结果= NULL;
SoapObject要求=新SoapObject(NAME_SPACE,METHOD_NAME); SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = TRUE;
envelope.setOutputSoapObject(请求); 尝试
{
HttpTransportSE androidHttpTransportSE =新HttpTransportSE(URL,5000);
androidHttpTransportSE.call(SOAP_ACTION,信封); 结果=(SoapObject)envelope.getResponse();
orderList = getOrder(结果); Log.v(服务器响应,result.toString());
}
赶上(例外五)
{
味精= Toast.makeText(这一点,连接服务器失败,Toast.LENGTH_SHORT);
msg.show();
} 味精= Toast.makeText(这一点,result.toString(),Toast.LENGTH_SHORT);
msg.show(); KitchenManager厨房=新KitchenManager();
kitchen.sendOrder(orderList);
}
赶上(例外五)
{
的System.out.println(捕获到异常:+ e.getMessage());
} 返回START_STICKY;
}
KitchenManager延伸活动
公共无效sendOrder(列表<排序> orderList)
{ this.orderList = orderList; 的for(int i = 0; I< orderList.size();我++)
{
的System.out.println(列出尺码:+ orderList.size());
的System.out.println(档案:+ orderList.get(ⅰ).getItem());
} 适配器=新OrdersAdapter(KitchenManager.this,R.layout.kitchen_order_list,this.orderList); 如果(this.orderList.size()== 0)
{ }
其他
{
list.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}
我在公共无效越来越例外 sendOrder()
行
适配器=新OrdersAdapter(KitchenManager.this,R.layout.kitchen_order_list,this.orderList);
请如果任何人能帮助我,因为我做了搜索,但无法找到解决我的问题。先谢谢了。
KitchenManager厨房=新KitchenManager();
不要试图做到这一点!
您无法使用新
实例化一个活动
。这不是Android的是如何工作的。
如果你想开始一个活动
从服务
你应该用做一个通知
和的PendingIntent
。
I'm getting this exception System services not available to Activities before onCreate()
when I'm trying to set adapter outside of my onCreate()
method.
I have tried using runOnUIThread()
function but still couldn't solve my problem.
Here is my code actually.
I'm sending a list from my Service subclass to my activity class via a method.
Class KitchenSerivce extends Service
@Override
public int onStartCommand(Intent intent, int flags , int startId)
{
super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "Service started ...", Toast.LENGTH_SHORT).show();
try
{
SoapObject result=null;
SoapObject request=new SoapObject(NAME_SPACE, METHOD_NAME);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
try
{
HttpTransportSE androidHttpTransportSE=new HttpTransportSE(URL, 5000);
androidHttpTransportSE.call(SOAP_ACTION, envelope);
result=(SoapObject)envelope.getResponse();
orderList=getOrder(result);
Log.v("Server response", result.toString());
}
catch (Exception e)
{
msg=Toast.makeText(this, "Connection to Server Failed", Toast.LENGTH_SHORT);
msg.show();
}
msg=Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT);
msg.show();
KitchenManager kitchen=new KitchenManager();
kitchen.sendOrder(orderList);
}
catch (Exception e)
{
System.out.println("Exception Caught:"+e.getMessage());
}
return START_STICKY;
}
KitchenManager extends Activity
public void sendOrder(List<Order> orderList)
{
this.orderList=orderList;
for(int i=0;i<orderList.size();i++)
{
System.out.println("List Size:"+orderList.size());
System.out.println("Item:"+orderList.get(i).getItem());
}
adapter=new OrdersAdapter(KitchenManager.this,R.layout.kitchen_order_list,this.orderList);
if(this.orderList.size()==0)
{
}
else
{
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
I'm getting exception in public void sendOrder()
on line
adapter=new OrdersAdapter(KitchenManager.this,R.layout.kitchen_order_list,this.orderList);
Please if any one could help me because I'm done searching but can't find solution to my problem. Thanks in advance.
KitchenManager kitchen=new KitchenManager();
Don't try to do this!!!
You cannot instantiate an Activity
using new
. This is NOT how Android works.
If you want to start an Activity
from a Service
you should do it using a Notification
and a PendingIntent
.
这篇关于不适用于活动的onCreate()之前系统服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!