2015-08-03 35 views
-1

之間的類中我試圖將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) 
+0

你能告訴顯示空指針哪一行? – Amsheer

+0

表達式'sharedPreferences.getString(「json」,null)''可能是'null'這肯定會拋出一個NPE。 – dotvav

+0

該錯誤顯示'NullPointerException'行發生'json.setJson(gson.toJson(companyInfo));'在'SaveInfo()' –

回答

1

您的jsonnull,因此錯誤。要onCreate添加

json = new JSON_String();

如果你想保留通過activites的對象,而不是失去它時,按下後退按鈕,你應該使用startActivityForResults()。這將需要你實現一些方法來處理活動之間傳遞的對象。 This is a good tutorial

+0

天哪謝謝,我覺得很傻,我沒有看到那個 –

+0

@JJStamp沒問題。保存人羣對於Parcelable的評論也是正確的,對於你的簡單對象來說這是不必要的。 – milez

-1

您是否嘗試過initlize JSON字符串?

+0

我在哪裏初始化它?如果我要在'OnCreate'中初始化字符串,那麼每次活動被破壞並重新創建時都不會覆蓋我的JSON字符串,就像按下後退按鈕一樣? –

+0

milez是對的,你應該接受他的回答 – user2145673

2

你想你的JSON轉換爲Parcelable:

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

只是作爲一個字符串傳遞,並在您RoomList.class從包獲得。用gson將它轉換成你的類並使用它。

+0

這是否仍然會通過按回來刪除,或者當按下後退按鈕時是否應該傳遞另一個包? –

+0

您正在將數據保存在onDestroy()中。如果您在所有使用相同對象並在onResume()中加載的活動中保存onPause(),則可以獲取對象的更改。 – savepopulation

0

問題是您沒有初始化您的JSON_String類。你需要初始化JSON_String

public JSON_String json = new JSON_String();