我收到此例外系統服務不可用於onCreate()
之前的活動,當我試圖在我的onCreate()
方法之外設置適配器時。
我試過使用runOnUIThread()
函數,但仍然無法解決我的問題。
這是我的代碼。
我通過一種方法將我的Service子類中的列表發送給我的活動類。系統服務不可用於onCreate之前的活動()
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延伸活動
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();
}
}
我上線
adapter=new OrdersAdapter(KitchenManager.this,R.layout.kitchen_order_list,this.orderList);
變得異常的公共無效sendOrder()
如有任何人都可以幫助我,因爲我已經完成搜索,但找不到解決方案。提前致謝。
如何處理sendOrder()內的上下文? –
檢查已更新的answer.pass將該上下文傳遞給OrdersAdapter。 –
@VipulShah:看到我的答案。OP正試圖用'new'創建一個'Activity'的實例。它永遠不會工作。 – Squonk