Java 尝试将JSON字符串保存到类中,以便在活动之间进行分组(NPE)

Java 尝试将JSON字符串保存到类中,以便在活动之间进行分组(NPE),java,android,json,android-studio,gson,Java,Android,Json,Android Studio,Gson,我试图将我的JSON字符串保存到一个类中,这样我就可以在活动之间传递该对象,并且在按下后退按钮时不会丢失任何数据。但是,当我尝试将字符串设置到对象中时,我收到一个NullPointerException。下面是发生异常的java文件、类java文件和错误的代码。我正在使用GSON进行序列化和反序列化。关于为什么会发生这种情况,有什么建议吗 NewLocation.java package com.customledsupply.ledaudit; import android.content

我试图将我的JSON字符串保存到一个类中,这样我就可以在活动之间传递该对象,并且在按下后退按钮时不会丢失任何数据。但是,当我尝试将字符串设置到对象中时,我收到一个
NullPointerException
。下面是发生异常的java文件、类java文件和错误的代码。我正在使用GSON进行序列化和反序列化。关于为什么会发生这种情况,有什么建议吗

NewLocation.java

package com.customledsupply.ledaudit;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Parcelable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


import com.google.gson.Gson;




public class NewLocation extends ActionBarActivity {

    public EditText editCoName;
    public EditText editCoAddress;
    public EditText editCoContact;
    public EditText editSqFt;
    public EditText editTaxed;
    public EditText editConcerns;
    public JSON_String json;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_location);
        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SaveInfo();
                Intent i = new Intent(NewLocation.this, RoomList.class);
                i.putExtra("json", (Parcelable) json);
                startActivity(i);
            }
        });

        editCoName = (EditText) findViewById(R.id.CoName);
        editCoAddress = (EditText) findViewById(R.id.CoAddress);
        editCoContact = (EditText) findViewById(R.id.CoContact);
        editSqFt = (EditText) findViewById(R.id.SqFt);
        editTaxed = (EditText) findViewById(R.id.Taxed);
        editConcerns = (EditText) findViewById(R.id.Concerns);

        SaveInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        LoadInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SaveInfo();
    }

    public void SaveInfo() {
        Gson gson = new Gson();
        CompanyInfo companyInfo = new CompanyInfo();

        companyInfo.setName(editCoName.getText().toString());
        companyInfo.setAddress(editCoAddress.getText().toString());
        companyInfo.setContact(editCoContact.getText().toString());
        companyInfo.setTaxed(editTaxed.getText().toString());
        companyInfo.setSqFt(editSqFt.getText().toString());
        companyInfo.setConcerns(editConcerns.getText().toString());

        json.setJson(gson.toJson(companyInfo));

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("json", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("json", json.getJson());
        editor.apply();
    }

    public void LoadInfo() {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("json", MODE_PRIVATE);
        json.setJson(sharedPreferences.getString("json", null));

        Gson gson = new Gson();
        CompanyInfo companyInfo = gson.fromJson(json.getJson(), CompanyInfo.class);
        if (companyInfo != null) {
            editCoName.setText(companyInfo.getName());
            editCoAddress.setText(companyInfo.getAddress());
            editCoContact.setText(companyInfo.getContact());
            editTaxed.setText(companyInfo.getTaxed());
            editSqFt.setText(companyInfo.getSqFt());
            editConcerns.setText(companyInfo.getConcerns());
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_new_location, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();


        switch(item.getItemId())
        {
            case R.id.home:
                startActivity(new Intent(getApplicationContext(), MainPage.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
JSON_String.java

package com.customledsupply.ledaudit;

public class JSON_String {

        private String json;

        public void setJson(String json) {
            this.json = json;
        }

        public String getJson() {
            return json;
        }


    }
NPE误差

08-03 08:46:52.081  24423-24423/com.customledsupply.ledaudit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.customledsupply.ledaudit, PID: 24423
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.customledsupply.ledaudit/com.customledsupply.ledaudit.NewLocation}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5061)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.customledsupply.ledaudit.NewLocation.SaveInfo(NewLocation.java:78)
            at com.customledsupply.ledaudit.NewLocation.onCreate(NewLocation.java:52)
            at android.app.Activity.performCreate(Activity.java:5237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5061)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
            at dalvik.system.NativeStart.main(Native Method)

您是否尝试初始化json字符串?

您的
json
null
,因此出现错误。要创建
onCreate
add

json=new json_String()


如果要在活动之间不断传递对象,并且在按下“上一步”按钮时不会丢失对象,则应使用
startActivityForResults()
。这将要求您实现两个方法,负责在活动之间传递对象

您正试图将json强制转换为Parcelable:

i.putExtra("json", (Parcelable) json);

只需将其作为字符串传递到您的RoomList中。类从bundle中获取。使用gson将其强制转换到类中并使用它。

问题是您没有初始化
JSON\u字符串。您需要初始化
JSON\u字符串


public JSON_String JSON=new JSON_String()

你能告诉我哪一行显示空指针吗?表达式
SharedReferences.getString(“json”,null)
可能是
null
,这肯定会抛出一个NPE。错误显示
NullPointerException
发生在
json.setJson(gson.toJson(companyInfo))
SaveInfo()中
在哪里初始化它?如果我在
OnCreate
中初始化字符串,那么在每次销毁和重新创建活动时(如按下“后退”按钮时),这不会覆盖我的JSON字符串吗?@JJStamp没问题。savepopulation关于Parcelable的评论也是正确的,对于您的简单对象来说,它是不必要的。按下“后退”按钮时,这不是仍然会被删除吗?还是应该传递另一个包?您正在onDestroy()中保存数据。如果将使用同一对象的所有活动都保存在onPause()中并加载到onResume()中,则可以获得对象的更改。