调用另一个类方法onPostExecute引发NullPointerException异常

编程入门 行业动态 更新时间:2024-10-22 09:28:55
本文介绍了调用另一个类方法onPostExecute引发NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这可能会帮助别人也: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异常

本文发布于:2023-11-11 23:30:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579839.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   方法   onPostExecute   NullPointerException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!