之間的類中我試圖將JSON字符串保存到類中,以便可以在活動之間傳遞該對象,並且在按下後退按鈕時不會丟失任何數據。但是,當我嘗試將字符串設置到對象中時,我收到了NullPointerException
。這裏是發生異常的java文件,類java文件和錯誤的代碼。我正在使用GSON來序列化和反序列化。有關爲何發生這種情況的任何建議?試圖將JSON字符串保存到活動(NPE)
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)
你能告訴顯示空指針哪一行? – Amsheer
表達式'sharedPreferences.getString(「json」,null)''可能是'null'這肯定會拋出一個NPE。 – dotvav
該錯誤顯示'NullPointerException'行發生'json.setJson(gson.toJson(companyInfo));'在'SaveInfo()' –