这也可以帮助某人:How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
我正在调用onPostExecute()
中另一个班级的方法。
我认为在onPostExecute()
之后调用doInBackground(String... params)
,根据文档和调试器,这是正确的。
调用方法:
protected void onPostExecute(String result) {
CreateHangOut crtHO = new CreateHangOut();
crtHO.createHangOut(result);
}
调用方法的一部分,导致NPE(方法的第一行):
public void createHangOut(String location) {
String city=autocompleteTV.getText().toString();
}
自动填充TextView
(autocompleteTV
)已在活动的创建时初始化。
以下是我致电AsyncTask
的方式:
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new
HTTPRequest().execute((autocompleteTV.getText()).toString());
}
});
调用onCreate的方法(单击按钮的活动):
private void initialize() {
gAPI= new GoogleAPIAutocomplete();
autocompleteTV = (AutoCompleteTextView)
findViewById(R.id.crtHOLocOptionsTV);
setUpAutocomplete();
create = (Button) findViewById(R.id.crtHOCreateBtn);
name =(EditText) findViewById(R.id.crtHONameET);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new
HTTPRequest().execute((autocompleteTV.getText()).toString());
}
});
}
答案 0 :(得分:1)
因为createHangOut
方法在CreateHangOut
Activity中所以不需要为访问方法创建新对象,只要使用方法名称调用它,如果扩展AsyncTask
类的类是CreateHangOut
的内部类1}}:
protected void onPostExecute(String result) {
CreateHangOut.this.createHangOut(result);
}