我目前正在尝试开发一个Android程序,需要从服务器读取json数据。我正在使用PHP中间层。而且我可以毫无问题地从服务器接收数据。但是,当我需要使用数据时,需要将从服务器(PHPconn.java)接收到的java类连接到我的活动(ProductInfo.java)中,但是会出现错误。
由于接收者代码是异步的,所以我在活动中创建了一个函数,将这些数据打印到屏幕上。我在postexecution()函数的末尾调用该函数。
我收到以下错误消息。
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
在线:
setContentView(R.layout.activity_product_info);
PHPcomm.java:
/*
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
/*
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);
*/
}
}
答案 0 :(得分:0)
您要多次设置setContentView(R.layout.activity_product_info);
。它不是必需的。仅将其保留在onCreate
中,因为setContentView将覆盖布局,并用新的布局替换。
对于PHPcomm.java
,请删除setContentView
中super.onStart();
之后Name.setText( name);
之前的onStart()
也为ProductInfo.java
做。