尝试将 JSON 字符串保存到 class 中以在活动之间进行分割(NPE)
Trying to save JSON string into a class to parce between activities (NPE)
我正在尝试将我的 JSON 字符串保存到 class 中,这样我就可以在活动之间传递该对象,并且在按下后退按钮时不会丢失任何数据。然而,当我尝试将字符串设置到对象中时,我收到了 NullPointerException
。这是发生异常的 java 文件、class 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.access0(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.access0(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
添加
json = new JSON_String();
如果你想在活动之间继续传递对象,并且在按下后退按钮时不丢失它,你应该使用startActivityForResults()
。这将需要您实现几个方法来处理在活动之间传递对象。 This is a good tutorial.
您正在尝试将您的 json 转换为 Parcelable:
i.putExtra("json", (Parcelable) json);
只需作为字符串传递并在您的 RoomList.class 中从捆绑包中获取。使用 gson 将其投射到您的 class 并使用它。
问题是您没有初始化 JSON_String
class。您需要像
一样初始化 JSON_String
public JSON_String json = new JSON_String();
我正在尝试将我的 JSON 字符串保存到 class 中,这样我就可以在活动之间传递该对象,并且在按下后退按钮时不会丢失任何数据。然而,当我尝试将字符串设置到对象中时,我收到了 NullPointerException
。这是发生异常的 java 文件、class 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.access0(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.access0(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
添加
json = new JSON_String();
如果你想在活动之间继续传递对象,并且在按下后退按钮时不丢失它,你应该使用startActivityForResults()
。这将需要您实现几个方法来处理在活动之间传递对象。 This is a good tutorial.
您正在尝试将您的 json 转换为 Parcelable:
i.putExtra("json", (Parcelable) json);
只需作为字符串传递并在您的 RoomList.class 中从捆绑包中获取。使用 gson 将其投射到您的 class 并使用它。
问题是您没有初始化 JSON_String
class。您需要像
JSON_String
public JSON_String json = new JSON_String();