从onPostExecute中的另一个类调用方法导致nullPointerException

时间:2015-01-16 18:01:54

标签: java android android-asynctask nullpointerexception

这也可以帮助某人: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();
   }

自动填充TextViewautocompleteTV)已在活动的创建时初始化。

以下是我致电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());
            }
    });
}

1 个答案:

答案 0 :(得分:1)

因为createHangOut方法在CreateHangOut Activity中所以不需要为访问方法创建新对象,只要使用方法名称调用它,如果扩展AsyncTask类的类是CreateHangOut的内部类1}}:

protected void onPostExecute(String result) {
    CreateHangOut.this.createHangOut(result);
}