首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将异步方法的值写入textbox时发生Android studio错误

将异步方法的值写入textbox时发生Android studio错误
EN

Stack Overflow用户
提问于 2018-06-24 05:48:55
回答 1查看 49关注 0票数 0

我目前正在尝试开发一个android程序需要从服务器读取json数据。我使用PHP中间层来实现这一点。而且我可以毫无问题地从服务器接收数据。但是,当我需要使用数据时,我需要将从服务器(PHPconn.java)接收到的java类连接到我的activity (ProductInfo.java),因为我得到了一个错误。

由于接收器代码以异步方式工作,因此我在activity中创建了一个函数,将这些数据打印到屏幕上。我在postexecution()函数的末尾调用该函数。

我得到以下错误。

java.lang.NullPointerException:尝试对null对象引用调用虚拟方法“”android.view.Window$Callback android.view.Window.getCallback()“”

在线上:

setContentView(R.layout.activity_product_info);

PHPcomm.java:

代码语言:javascript
复制
/*
PAUSE FOR PRAYER

Dear God,
Please help me find any malicious bugs that I've created
and ,yet, I can't spot.
Make my code free of errors.


END PRAYER
*/

package gq.yigit.foodcloud;

import android.content.Intent;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

import com.google.api.client.json.JsonObjectParser;
import com.google.firebase.database.*;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import android.view.View.OnClickListener;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

public class ProductInfo extends AppCompatActivity implements OnClickListener {
    private static final String TAG = "MainActivity";
    private TextView Name;
    private TextView Cal;
    private TextView Cooked;
    private TextView Nutrients;
    private TextView BBD;
    private TextView Processed;
    private TextView Problematic;
    private TextView Allergens;
    private String name;
    private String  cal;
    private String cooked;
    private String nutrients;
    private ArrayList nutrients_array;
    private String bbd;
    private String processed;
    private String expiry_date;
    private String allergens;
    private ArrayList allergens_array;
    public String json_str;
    public JSONObject Prod;
    private Button scanBtn;
    private Button jrnyBtn;
    public String prod_loc;
    public String allergens_print = new String();
    public String nutrients_print = new String();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_info);


    }

    @Override
    public void onClick(View v){
        if (v.getId() == R.id.button) {
            Intent i = new Intent(ProductInfo.this, MainActivity.class);
            startActivity(i);
        }if (v.getId() == R.id.journey) {
            Intent i = new Intent(ProductInfo.this, LearnMore.class);
            i.putExtra("key", prod_loc);
            startActivity(i);
        }
    }

    public void onStart(){
        super.onStart();
        setContentView(R.layout.activity_product_info);
        scanBtn = (Button)findViewById(R.id.button);
        scanBtn.setOnClickListener(this);
        jrnyBtn = (Button)findViewById(R.id.journey);
        jrnyBtn.setOnClickListener(this);


                Bundle extras = getIntent().getExtras();
                if (extras != null) {
                    prod_loc = extras.getString("key");
                }

                PHPComm comm = new PHPComm(this);
                comm.execute("get", "1", "Products");

            }

            public void continueApp(String json_str){
                try {
                    Log.d(TAG,"This is a pointer");
                    Log.d(TAG,"Got json str " + json_str);
                    JSONObject jsonObj = new JSONObject(json_str);
                    name = jsonObj.get("Prod_Name").toString();
               /*     cal = jsonObj.getJSONArray("Calories").toString();
                    cooked = jsonObj.getJSONArray("Cooked").toString();
                    nutrients = jsonObj.getJSONArray("Nutrients").toString();
                    bbd = jsonObj.getJSONArray("BBD").toString();
                    processed = jsonObj.getJSONArray("Process").toString();
                    //expiry_date = jsonObj.getJSONArray("ED").toString();
                    allergens = jsonObj.getJSONArray("Allergens").toString();
                    */
                }catch (JSONException e) {
                    Log.d(TAG, "An error occured with the json!");
                }catch (NullPointerException e){
                    Log.d(TAG,"Received null data!");
                }
                /*
                if(allergens.isEmpty()) {
                    Allergens.setText("Allergens : None");
                }else{
                    for(int i = 0; i < allergens.size();i++) {
                        allergens_print = allergens_print + allergens.get(i);
                        if(i != allergens.size() -1){
                            allergens_print = allergens_print + " , ";
                        }
                    }
                    Allergens.setText("Allergens : " + allergens_print);
                }
                Nutrients = (TextView) findViewById(R.id.nutrients);
                nutrients_print = "";
                if(nutrients.isEmpty()) {
                    Nutrients.setText("Nutrients : None");
                }else{
                    for(int i = 0; i < nutrients.size();i++) {
                        nutrients_print = nutrients_print + nutrients.get(i);
                        if(i != nutrients.size() -1){
                            nutrients_print = nutrients_print + " , ";
                        }
                    }
                    Nutrients.setText("Nutrients : " + nutrients_print);
                }
                */
                setContentView(R.layout.activity_product_info);
                Name.setText( name);
/*                Cal = (TextView) findViewById(R.id.Calories);
                Cal.setText(cal);
                Allergens = (TextView) findViewById(R.id.allergens);
                allergens_print = "";
                Cooked = (TextView) findViewById(R.id.cooked);
                Cooked.setText("Cooked : " + cooked);
                BBD = (TextView) findViewById(R.id.BBD);
                BBD.setText(bbd);
                Processed = (TextView) findViewById(R.id.Process);
                Processed.setText("Process : " + processed);
*/
            }








    }

ProductInfo.java

代码语言:javascript
复制
/*
PAUSE FOR PRAYER

Dear God,
Please help me find any malicious bugs that I've created
and ,yet, I can't spot.
Make my code free of errors.


END PRAYER
*/

package gq.yigit.foodcloud;

import android.content.Intent;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

import com.google.api.client.json.JsonObjectParser;
import com.google.firebase.database.*;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import android.view.View.OnClickListener;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

public class ProductInfo extends AppCompatActivity implements OnClickListener {
    private static final String TAG = "MainActivity";
    private TextView Name;
    private TextView Cal;
    private TextView Cooked;
    private TextView Nutrients;
    private TextView BBD;
    private TextView Processed;
    private TextView Problematic;
    private TextView Allergens;
    private String name;
    private String  cal;
    private String cooked;
    private String nutrients;
    private ArrayList nutrients_array;
    private String bbd;
    private String processed;
    private String expiry_date;
    private String allergens;
    private ArrayList allergens_array;
    public String json_str;
    public JSONObject Prod;
    private Button scanBtn;
    private Button jrnyBtn;
    public String prod_loc;
    public String allergens_print = new String();
    public String nutrients_print = new String();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_info);


    }

    @Override
    public void onClick(View v){
        if (v.getId() == R.id.button) {
            Intent i = new Intent(ProductInfo.this, MainActivity.class);
            startActivity(i);
        }if (v.getId() == R.id.journey) {
            Intent i = new Intent(ProductInfo.this, LearnMore.class);
            i.putExtra("key", prod_loc);
            startActivity(i);
        }
    }

    public void onStart(){
        super.onStart();
        setContentView(R.layout.activity_product_info);
        scanBtn = (Button)findViewById(R.id.button);
        scanBtn.setOnClickListener(this);
        jrnyBtn = (Button)findViewById(R.id.journey);
        jrnyBtn.setOnClickListener(this);


                Bundle extras = getIntent().getExtras();
                if (extras != null) {
                    prod_loc = extras.getString("key");
                }

                PHPComm comm = new PHPComm(this);
                comm.execute("get", "1", "Products");

            }

            public void continueApp(String json_str){
                try {
                    Log.d(TAG,"This is a pointer");
                    Log.d(TAG,"Got json str " + json_str);
                    JSONObject jsonObj = new JSONObject(json_str);
                    name = jsonObj.get("Prod_Name").toString();
               /*     cal = jsonObj.getJSONArray("Calories").toString();
                    cooked = jsonObj.getJSONArray("Cooked").toString();
                    nutrients = jsonObj.getJSONArray("Nutrients").toString();
                    bbd = jsonObj.getJSONArray("BBD").toString();
                    processed = jsonObj.getJSONArray("Process").toString();
                    //expiry_date = jsonObj.getJSONArray("ED").toString();
                    allergens = jsonObj.getJSONArray("Allergens").toString();
                    */
                }catch (JSONException e) {
                    Log.d(TAG, "An error occured with the json!");
                }catch (NullPointerException e){
                    Log.d(TAG,"Received null data!");
                }
                /*
                if(allergens.isEmpty()) {
                    Allergens.setText("Allergens : None");
                }else{
                    for(int i = 0; i < allergens.size();i++) {
                        allergens_print = allergens_print + allergens.get(i);
                        if(i != allergens.size() -1){
                            allergens_print = allergens_print + " , ";
                        }
                    }
                    Allergens.setText("Allergens : " + allergens_print);
                }
                Nutrients = (TextView) findViewById(R.id.nutrients);
                nutrients_print = "";
                if(nutrients.isEmpty()) {
                    Nutrients.setText("Nutrients : None");
                }else{
                    for(int i = 0; i < nutrients.size();i++) {
                        nutrients_print = nutrients_print + nutrients.get(i);
                        if(i != nutrients.size() -1){
                            nutrients_print = nutrients_print + " , ";
                        }
                    }
                    Nutrients.setText("Nutrients : " + nutrients_print);
                }
                */
                setContentView(R.layout.activity_product_info);
                Name.setText( name);
/*                Cal = (TextView) findViewById(R.id.Calories);
                Cal.setText(cal);
                Allergens = (TextView) findViewById(R.id.allergens);
                allergens_print = "";
                Cooked = (TextView) findViewById(R.id.cooked);
                Cooked.setText("Cooked : " + cooked);
                BBD = (TextView) findViewById(R.id.BBD);
                BBD.setText(bbd);
                Processed = (TextView) findViewById(R.id.Process);
                Processed.setText("Process : " + processed);
*/
            }








    }
EN

回答 1

Stack Overflow用户

发布于 2018-06-24 12:48:28

您正在多次设置setContentView(R.layout.activity_product_info);。这不是必需的。仅将其保留在onCreate中,因为setContentView将覆盖布局,并用新布局替换它。

对于PHPcomm.java,删除放置在onStart()super.onStart();之后和Name.setText( name);之前的setContentView

也可以为ProductInfo.java这样做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51005321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档