2015-08-03 36 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();