asynctask 函数无法识别结果值

Posted

技术标签:

【中文标题】asynctask 函数无法识别结果值【英文标题】:the asynctask function is not recognizing the result value 【发布时间】:2019-10-13 11:57:18 【问题描述】:

我通过 AsyncTask 访问 JSON,但是在 onPostExecute 中,String 结果没有得到结果值。

我希望它读取 json 并返回值“perfil”,然后将 new 发送到主要活动

归功于谷歌翻译......

访问 MainActivity 到 MecanismoString

String login = textousuario.getText().toString();
            String senha = textosenha.getText().toString();
            String urllogin = "http...."
            String tipo = "";
            MecanismoString mecanismoString = new MecanismoString(this, this);
            mecanismoString.execute(tipo, urllogin, login, senha);

Asynctask MecanismoString

public class MecanismoString extends AsyncTask <String, String, String> 

    private Interfacestring Interfacestring;
    private RequestQueue requestQueue;
    private StringRequest request;
    private Interfacestring execinterface;

    String usuario, senha, result;

    Context context;
    Toast toast;

    public MecanismoString (Context context, Interfacestring execinterface)
        this.context = context;
        this.Interfacestring = execinterface;
    




    @Override
    public String doInBackground(String... params) 

        Log.i("Script", "1 ->" + params[0]);
        Log.i("Script", "2 ->" + params[1]);
        Log.i("Script", "3 ->" + params[2]);
        Log.i("Script", "4 ->" + params[3]);

        usuario = params[2];
        senha = params[3];

        requestQueue = Volley.newRequestQueue(context);

        request = new StringRequest(Request.Method.POST, params[1], new Response.Listener<String>() 
            @Override
            public void onResponse(String response) 
                try 
                    JSONObject jsonObject = new JSONObject(response);

                    result = jsonObject.getString("perfil");


                 catch (JSONException e) 
                    e.printStackTrace();
                

            
        , new Response.ErrorListener() 
            @Override
            public void onErrorResponse(VolleyError error) 

            
        )
            @Override
            protected Map<String, String> getParams() throws AuthFailureError 
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("login", usuario);
                hashMap.put("senha", senha);

                return hashMap;
            
        ;

        requestQueue.add(request);

        return (result);
    


    @Override
    protected void onPostExecute(String result) 

        execinterface.carregarString(result);

    


返回 MecanismoString 值的代码

public void carregarString(String result) 

        Toast.makeText(context.getApplicationContext(),
                result,
                Toast.LENGTH_SHORT) .show();

    

错误日志

E/androidRuntime: FATAL EXCEPTION: main
    Process: ic.eng.br.eunaobra, PID: 13735
    java.lang.NullPointerException: Attempt to invoke interface method 'void ic.eng.br.eunaobra.Interfacestring.carregarString(java.lang.String)' on a null object reference
        at ic.eng.br.eunaobra.MecanismoString.onPostExecute(MecanismoString.java:94)
        at ic.eng.br.eunaobra.MecanismoString.onPostExecute(MecanismoString.java:22)
        at android.os.AsyncTask.finish(AsyncTask.java:695)
        at android.os.AsyncTask.-wrap1(Unknown Source:0)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
    (HTTPLog)-Static: isSBSettingEnabled false
I/qtaguid: Untagging socket 76 failed errno=-22
W/NetworkManagementSocketTagger: untagSocket(76) failed with errno -22
Application terminated.

【问题讨论】:

在 MecanismoString 构造函数中我认为你没有正确引用接口 试试这个公共 MecanismoString (Context context, Interfacestring execinterface) this.context = context; this.execinterface=执行接口; 也是一个错误,重复界面,但继续出现此错误:java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on空对象引用 【参考方案1】:

请传递正确的上下文和 execinterface 对象:

MecanismoString mecanismoString = new MecanismoString(MainActivity.this, correct execinterface object);

请从carregarString()中删除getApplicationContext(),因为您已经拥有context

 public void carregarString(String result) 
        Toast.makeText(context,result,Toast.LENGTH_SHORT) .show();
    

【讨论】:

声明对象而不是“this”时的错误,更改了“Toast”,但错误仍然存​​在:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android. content.Context.getPackageName()' 在 android.widget.Toast. 的空对象引用上 对不起,我看不懂你刚才写的东西,你能不能说得清楚一点? n 总结,我需要结果值,它位于 InBackground 内部,以便在 onPostExecute 中接收,但我做错了什么......我无法将字符串结果发送到 onPostExecute 你能不能返回 result 而不是 (result) ?并在回行处打断点,看看结果是否生成良好 我发现了错误,我卸载了整个过程。我在吐司。 ` toast = Toast.makeText(this, result, Toast.LENGTH_SHORT); toast.show();` 现在我正在重新插入 JSON

以上是关于asynctask 函数无法识别结果值的主要内容,如果未能解决你的问题,请参考以下文章

求助:IF函数识别不了是怎么回事?

excel if条件函数无法识别负号,求大神指教!

Eclipse CDT 编辑器无法识别 OpenGL 扩展

无法从 AsyncTask 返回值;使用接口和委托;

最小值无法识别

驱动器映射后,用户无法识别