gpt4 book ai didi

java - 无法在 setOnClickListener 内执行 Asynctask

转载 作者:行者123 更新时间:2023-12-02 02:39:47 25 4
gpt4 key购买 nike

我在这一行遇到错误 -> new GetHttpResponse(this).execute();

放在按钮外面不会报错

我从 http://www.android-examples.com/create-dynamic-listview-using-json-parsing-php-mysql/ 获得此代码我刚刚添加了按钮,以便在数据库更新时可以刷新它

 public class MainActivity extends Activity {

ListView listCollege;
ProgressBar proCollageList;
Button bton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
bton = (Button) findViewById(R.id.button);
listCollege = (ListView)findViewById(R.id.listCollege);
proCollageList = (ProgressBar)findViewById(R.id.proCollageList);
new GetHttpResponse(this).execute();

bton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "List is refreshed", Toast.LENGTH_SHORT).show();
new GetHttpResponse(this).execute();
}});
}



private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
private Context context;
String result;
List<benedict.com.listviewpractice.cources> collegeList;
public GetHttpResponse(Context context)
{
this.context = context;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0)
{
HttpService httpService = new HttpService("http://10.0.2.2/courses.php");
try
{
httpService.ExecutePostRequest();

if(httpService.getResponseCode() == 200)
{
result = httpService.getResponse();
Log.d("Result", result);
if(result != null)
{
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(result);

JSONObject object;
JSONArray array;
benedict.com.listviewpractice.cources college;
collegeList = new ArrayList<benedict.com.listviewpractice.cources>();
for(int i=0; i<jsonArray.length(); i++)
{
college = new benedict.com.listviewpractice.cources();
object = jsonArray.getJSONObject(i);

college.cources_name = object.getString("cource_name");
college.cources_description = object.getString("cources_description");

collegeList.add(college);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpService.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result)

{
proCollageList.setVisibility(View.GONE);
listCollege.setVisibility(View.VISIBLE);
if(collegeList != null)
{
ListAdapter adapter = new ListAdapter(collegeList, context);
listCollege.setAdapter(adapter);
}
}
}
}

最佳答案

在 OnClickListener 内部,this 表示 OnClickListener 的实例,而不是您期望的上下文对象,因此 this 应该是 MainActivity.this

new GetHttpResponse(MainActivity.this).execute();

关于java - 无法在 setOnClickListener 内执行 Asynctask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45685862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com