调用另一个类方法onPostExecute引发NullPointerException异常 [英] Calling method from another class in onPostExecute causing nullPointerException

查看:436
本文介绍了调用另一个类方法onPostExecute引发NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能会帮助别人也:<一href=\"http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a\">How获得OnPostExecute(),以主要活动的结果,因为AsyncTask的是一个单独的类?

我打电话从另一个类方法 onPostExecute()

我认为 onPostExecute()终止后,称为doInBackground(字符串... PARAMS),这是正确的根据文档和调试器。

调用方式:

 保护无效onPostExecute(字符串结果){
    CreateHangOut crtHO =新CreateHangOut();
    crtHO.createHangOut(结果);
}

方法的一部分调用,从而导致NPE(方法的第一行):

 公共无效createHangOut(字符串位置){
    。字符串城市= autocompleteTV.getText()的toString();
   }

自动完成的TextView autocompleteTV )被初始化活动的onCreate。

下面是我如何调用的AsyncTask

  create.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            新
。HTT prequest()执行((autocompleteTV.getText())的toString());
            }
    });

调用的方法的onCreate(从按钮被点击的地方活动):

 私人无效的initialize(){
    加皮=新GoogleAPIAutocomplete();
    autocompleteTV =(AutoCompleteTextView)
    findViewById(R.id.crtHOLocOptionsTV);
    setUpAutocomplete();
    创建=(按钮)findViewById(R.id.crtHOCreateBtn);
    名称=(EditText上)findViewById(R.id.crtHONameET);
    create.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            新
    。HTT prequest()执行((autocompleteTV.getText())的toString());
            }
    });
}


解决方案

由于 createHangOut 方法是在 CreateHangOut 活动所以无需访问方法,只是把它的使用方法名称,如果延伸的AsyncTask 类类是内部类 CreateHangOut

 保护无效onPostExecute(字符串结果){
    CreateHangOut.this.createHangOut(结果);
}

This might help someone also: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

I am calling method from another class in onPostExecute().

I assume that onPostExecute() is called after doInBackground(String... params) and that is right, according to documentation and debugger.

Calling the method:

protected void onPostExecute(String result) {
    CreateHangOut crtHO = new CreateHangOut();
    crtHO.createHangOut(result);
}

Part of method called, causing NPE (first line of method):

public void createHangOut(String location) {
    String city=autocompleteTV.getText().toString();
   }

Autocomplete TextView(autocompleteTV) is initialized onCreate of the activity.

Here is how I call AsyncTask:

create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new 
HTTPRequest().execute((autocompleteTV.getText()).toString());
            }
    });

Method called onCreate (of activity from where button is clicked) :

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());
            }
    });
}

解决方案

Because createHangOut method is in CreateHangOut Activity so no need to create new object for accessing method just call it using method name if class which extends AsyncTask class is inner class of CreateHangOut :

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

这篇关于调用另一个类方法onPostExecute引发NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆