2017-02-19 46 views
0

我已经查看了StackOverflow上的所有答案,但没有计算出为什么我会收到错误:由java.lang.NullPointerException引起的:尝试在空对象引用上调用虚拟方法'java.lang.String android.content.Context.getPackageName()'。我下载了gson2.2.2,并将编译行放入了build.gradle。在SharedPreferences中保存对象并从Android Studio中的活动中读取它使用GSON

这是我救了我的对象(客户)类的代码:

public class CreateAccount extends AppCompatActivity { 

    SharedPreferences accountsPref = getPreferences(MODE_PRIVATE); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_create_account); 
     setTitle("Create New Account"); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    } 

    @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_create_account, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      //back button 
      case android.R.id.home: 
       // app icon in action bar clicked; go home 
       Intent intent = new Intent(this, MainScreen.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
       return true; 

      //save button 
      case R.id.action_menu_save: 

       // get EditText by id 
       EditText inputTxt1 = (EditText) findViewById(R.id.AccountName); 
       // Store EditText in Variable 
       String name = inputTxt1.getText().toString(); 

       EditText inputTxt2 = (EditText) findViewById(R.id.StartingBalance); 
       double balance = Double.parseDouble(inputTxt2.getText().toString()); 

       Account newAccount = new Account(name, balance); 

       SharedPreferences.Editor editor = accountsPref.edit(); 
       Gson gson = new Gson(); 
       String json = gson.toJson(newAccount); 
       editor.putString("newAccount", json); 
       editor.commit(); 

       Intent intent2 = new Intent(this, MainScreen.class); 
       intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent2); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 

而这正是我试图加载对象的活动:

package com.cashtrack.kennethlee.cashtrack; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

import com.google.gson.Gson; 

import java.io.FileInputStream; 
import java.io.StringWriter; 
import java.util.ArrayList; 

public class MainScreen extends AppCompatActivity { 
    //array list of accounts 
    ArrayList<Account> accounts = new ArrayList<>(); 

    SharedPreferences accountsPref = getPreferences(MODE_PRIVATE); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_screen); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       startActivity(new Intent(MainScreen.this, CreateAccount.class)); 
      } 
     }); 
    } 

    @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_main_screen, 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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     Gson gson = new Gson(); 
     String json = accountsPref.getString("newAccount", ""); 
     Account newAccount = gson.fromJson(json, Account.class); 
     //accounts.add(newAccount); 
    } 
} 
+3

举动'accountsPref =的getPreferences(MODE_PRIVATE);''里面oncreate' –

+0

我花了6个小时这个... LOL感谢这么多。 –

+0

欢迎您,快乐编码 –

回答

1

需要初始化SharedPreferences accountsPref双方你的活动的onCreate()内。

斯普利特这行代码为两个语句: SharedPreferences accountsPref = getPreferences(MODE_PRIVATE);

  • SharedPreferences accountsPref在你拥有它目前全球范围。

  • accountsPref = getPreferences(MODE_PRIVATE);onCreate()

onCreate()是Android框架告诉您您的活动已初始化的地方。

相关问题