gpt4 book ai didi

java - 在 SharedPreferences 中保存对象并使用 GSON 从 Android Studio 中的 Activity 中读取该对象

转载 作者:行者123 更新时间:2023-12-01 08:52:03 25 4
gpt4 key购买 nike

我已经浏览了 StackOverflow 上的所有答案,但没有弄清楚为什么会出现错误:原因是:java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.String android.content空对象引用上的“.Context.getPackageName()”。我下载了 gson2.2.2 并将编译行放在 build.gradle 中。

这是我保存对象(帐户)的类的代码:

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);
}
}
}

这是我尝试加载对象的 Activity :

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);
}
}

最佳答案

您需要在两个 Activity 的 onCreate() 中初始化 SharedPreferencesaccountsPref

将这行代码分成两条语句:SharedPreferences accountPref = getPreferences(MODE_PRIVATE);

  • SharedPreferencesaccountsPref 在您当前拥有的全局范围内。

  • accountsPref = getPreferences(MODE_PRIVATE);onCreate() 内。

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

关于java - 在 SharedPreferences 中保存对象并使用 GSON 从 Android Studio 中的 Activity 中读取该对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42325115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com