应用程序崩溃,方法是:单击“保存”按钮,我在此程序中使用了共享首选项和GSON使用了2个活动。在第一个活动中,我使用了4个 EditText 和一个 Button < / strong>(提交),这将导致第一个活动转为第二个活动,在该活动中我做了2个按钮(保存)(加载)。在第一个活动中,一切正常,但是只要我单击第二个活动保存按钮最终错误显示为app_Closed。 我是Android开发领域的初学者。 高级请来帮我!!
package com.example.rc.sharprejson;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main2Activity extends AppCompatActivity {
EditText Name, Profession, ProfId, Role;
Button Save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Name = (EditText) findViewById(R.id.editText);
Profession = (EditText) findViewById(R.id.editText2);
ProfId = (EditText) findViewById(R.id.editText3);
Role = (EditText) findViewById(R.id.editText4);
Save = (Button) findViewById(R.id.button);
Save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
Main2Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
第二次活动
package com.example.rc.sharprejson;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private TextView txvDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txvDisplay = (TextView) findViewById(R.id.txvdisplay);
}
public void saveObjectType(View view) {
Main2Activity employee = getEmployee();
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String jsonString = gson.toJson( employee, Main2Activity.class);
Log.i(TAG + " Save", jsonString);
editor.putString("employee_key", jsonString);
editor.apply();
}
public void loadObjectType(View view) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
String jsonString = sharedPreferences.getString("employee_key","N/A");
Log.i(TAG + " Load", jsonString);
Gson gson = new Gson();
Main2Activity employee = gson.fromJson(jsonString,Main2Activity.class);
String Displaytext = employee.Name.getText().toString() + " \n " + employee.Profession.getText().toString() + " \n "
+ employee.ProfId.getText().toString();
txvDisplay.setText(Displaytext);
}
public Main2Activity getEmployee(){
Main2Activity employee = new Main2Activity();
employee.Name.getText().toString();
employee.Profession.getText().toString();
employee.ProfId.getText().toString();
return employee;
} `