Java 无法从AsyncTask填充ListView
错误是onCreate()之前的活动无法使用系统服务 下面是我的AsyncTask(它位于另一个类的单独文件中) GetWarehouseSales.javaJava 无法从AsyncTask填充ListView,java,android,listview,android-asynctask,Java,Android,Listview,Android Asynctask,错误是onCreate()之前的活动无法使用系统服务 下面是我的AsyncTask(它位于另一个类的单独文件中) GetWarehouseSales.java public class GetWarehouseSales extends Activity { private String TAG = GetWarehouseSales.class.getSimpleName(); private ListView lv; private ProgressDialog p
public class GetWarehouseSales extends Activity {
private String TAG = GetWarehouseSales.class.getSimpleName();
private ListView lv;
private ProgressDialog pDialog;
private Activity activityContext;
//URL to get JSON details
private static String url = "http://192.168.0.1/mycc/retrieve_ws.php";
ArrayList<HashMap<String,String>> sales_details;
public GetWarehouseSales(Activity context){
this.activityContext = context;
sales_details = new ArrayList<>();
}
public void executeGWS(){
new RetrieveWarehouseSalesTask().execute();
}
public class RetrieveWarehouseSalesTask extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(activityContext);
pDialog.setMessage("Getting you the best warehouse sales...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0){
HttpHandler sh = new HttpHandler();
//making a request to URL and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if(jsonStr != null){
try{
JSONObject jsonObj = new JSONObject(jsonStr);
//Getting JSON Array Node
JSONArray sales = jsonObj.getJSONArray("Result");
//looping through all results
for(int i = 0; i<sales.length();i++){
JSONObject s = sales.getJSONObject(i);
String title = s.getString("title");
String description = s.getString("description");
HashMap<String,String> salesDetails = new HashMap<>();
//adding each child node to HashMap key =>value
salesDetails.put("title",title);
salesDetails.put("description",description);
//adding to array list
sales_details.add(salesDetails);
}
Log.d("TAG",sales_details.toString());
}catch(final JSONException e){
Log.e(TAG, "JSON parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"JSON parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}else{
Log.e(TAG,"Couldn't get json from server");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Check logcat", Toast.LENGTH_LONG ).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
if(pDialog.isShowing()){
pDialog.dismiss();
}
//update parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(GetWarehouseSales.this, sales_details,R.layout.item_listview, new String[]{
"title","description"}, new int[]{R.id.text,R.id.description});
lv = (ListView)findViewById(R.id.list_item);
lv.setAdapter(adapter);
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
new GetWarehouseSales(this).executeGWS();
}
错误指向这一行:
ListAdapter adapter = new SimpleAdapter(GetWarehouseSales.this, sales_details,R.layout.item_listview, new String[]{
"title","description"}, new int[]{R.id.text,R.id.description});
我正在尝试将数据填充到listview中,但无法执行此操作。尝试替换
new GetWarehouseSales(this).executeGWS();
到
一般来说,你做这件事的方式是错误的。正如我在这里已经回答的,您应该在main活动中定义AsynkTask
,尝试替换
new GetWarehouseSales(this).executeGWS();
到
一般来说,你做这件事的方式是错误的。正如我在这里已经回答的,您应该在MainActivity
中定义AsynkTask
,正如Vasily Kabunov指出的,不要创建类的新实例来调用该方法。当活动不是活动时,也不要扩展它
但另一个主要问题是doInBackground
应该建立一个结果,然后通过写入活动的一个字段(sales\u details
)安全地传递给onPostExecute
),您已经使这个非线程安全
因此,不要访问sales\u details
或doInBackground
中的其他活动字段,请执行以下操作:
public class RetrieveWarehouseSalesTask extends AsyncTask<Void,Void,ArrayList<HashMap<String,String>>>{ //return type set
@Override
protected ArrayList<HashMap<String,String>> doInBackground(Void... arg0){
ArrayList<HashMap<String,String>> result = new ArrayList<>();
...
//adding to array list
result.add(salesDetails);
...
return result;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String,String>> result){
super.onPostExecute(result);
if(pDialog.isShowing()){
pDialog.dismiss();
}
//update parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(GetWarehouseSales.this, result,R.layout.item_listview, new String[]{
"title","description"}, new int[]{R.id.text,R.id.description});
lv = (ListView)findViewById(R.id.list_item);
lv.setAdapter(adapter);
}
}
公共类RetrieveWarehouseSalesTask扩展了AsyncTask{//返回类型集
@凌驾
受保护的ArrayList doInBackground(无效…arg0){
ArrayList结果=新建ArrayList();
...
//添加到数组列表
结果。添加(salesDetails);
...
返回结果;
}
@凌驾
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
if(pDialog.isShowing()){
pDialog.disclose();
}
//将解析的JSON数据更新到ListView中
ListAdapter=new SimpleAdapter(GetWarehouseSales.this,result,R.layout.item_listview,新字符串[]){
“title”、“description”},新int[]{R.id.text,R.id.description});
lv=(ListView)findViewById(R.id.list\u项);
低压设置适配器(适配器);
}
}
正如Vasily Kabunov指出的,不要创建类的新实例来调用该方法。当活动不是活动时,也不要扩展它
但另一个主要问题是doInBackground
应该建立一个结果,然后通过写入活动的一个字段(sales\u details
)安全地传递给onPostExecute
),您已经使这个非线程安全
因此,不要访问sales\u details
或doInBackground
中的其他活动字段,请执行以下操作:
public class RetrieveWarehouseSalesTask extends AsyncTask<Void,Void,ArrayList<HashMap<String,String>>>{ //return type set
@Override
protected ArrayList<HashMap<String,String>> doInBackground(Void... arg0){
ArrayList<HashMap<String,String>> result = new ArrayList<>();
...
//adding to array list
result.add(salesDetails);
...
return result;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String,String>> result){
super.onPostExecute(result);
if(pDialog.isShowing()){
pDialog.dismiss();
}
//update parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(GetWarehouseSales.this, result,R.layout.item_listview, new String[]{
"title","description"}, new int[]{R.id.text,R.id.description});
lv = (ListView)findViewById(R.id.list_item);
lv.setAdapter(adapter);
}
}
公共类RetrieveWarehouseSalesTask扩展了AsyncTask{//返回类型集
@凌驾
受保护的ArrayList doInBackground(无效…arg0){
ArrayList结果=新建ArrayList();
...
//添加到数组列表
结果。添加(salesDetails);
...
返回结果;
}
@凌驾
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
if(pDialog.isShowing()){
pDialog.disclose();
}
//将解析的JSON数据更新到ListView中
ListAdapter=new SimpleAdapter(GetWarehouseSales.this,result,R.layout.item_listview,新字符串[]){
“title”、“description”},新int[]{R.id.text,R.id.description});
lv=(ListView)findViewById(R.id.list\u项);
低压设置适配器(适配器);
}
}
doInBackground
应该建立一个结果,然后通过写入活动的一个字段(sales\u details
)安全地传递到onPostExecute
),您已经将此设置为非线程安全。为什么GetWarehouseSales要扩展活动?行lv=(ListView)findViewById(R.id.list\u项);很可能也会失败。doInBackground
应该建立一个结果,然后通过写入活动的一个字段(sales\u details
)安全地传递给onPostExecute
),您已经将此设置为非线程安全的。为什么GetWarehouseSales会扩展活动?行lv=(ListView)findViewById(R.id.list\u项);很可能也会失败。原因是我将来可能会添加更多异步任务。因此,我希望保持MainActivity尽可能少的代码。因此,我在单独的java文件中创建了AsyncTask。是否有其他方法可以解决此问题?请尝试使AsyncTask
static:公共静态类RetrieveWarehouseSalesTask扩展,然后新建GetWarehouseSales.RetrieveWarehouseSalesTask().execute()代码>原因是我将来可能会添加更多异步任务。因此,我希望保持MainActivity尽可能少的代码。因此,我在单独的java文件中创建了AsyncTask。是否有其他方法可以解决此问题?请尝试使AsyncTask
static:公共静态类RetrieveWarehouseSalesTask扩展,然后新建GetWarehouseSales.RetrieveWarehouseSalesTask().execute()代码>@kylas这对你有帮助吗?@kylas这对你有帮助吗?