-3
我想了解Java序列化是如何工作的,所以我試圖添加到我自己的Android應用程序中傳輸兩個字符串的可能性(First和姓氏,確切地說),然後在另一個活動中顯示兩個字符串的簡單「歡迎」消息。 事情開始變得非常奇怪,因爲我實際上設法轉移這兩個字符串,但是當涉及到顯示它們時,我「失去」它們,並且我得到一個NullPointerException!Android - 試圖調用getSerializableExtra()方法(它的工作原理)後的NullPointerException
總之,這裏的代碼:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button personalData;
protected String strUsername;
private boolean isProfileReady;
TextView welcome;
protected SharedPreferences loginData;
SharedPreferences wizardPrefs;
SharedPreferences.Editor wizardEditor;
DataUser userData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
welcome = (TextView) findViewById(R.id.welcome);
personalData = (Button) findViewById(R.id.data);
personalData.setText("Personal Data");
personalData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, WizardActivity.class);
intent.putExtra("profileReady", isProfileReady);
startActivity(intent);
}
});
loginData = getSharedPreferences("UserPrefs", MODE_PRIVATE);
wizardPrefs = this.getSharedPreferences("USERDATA", MODE_PRIVATE);
String value = loginData.getString("Username", null);
welcome.setText("Welcome " + loginData.getString("Username", strUsername) + "!");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
isProfileReady = true;
personalData.setText("My Profile");
welcome.setText("Welcome " + userData.getStrFName() + " " + userData.getStrLName() + "!");
}
}
}
WizardActivity.java
public class WizardActivity extends AppCompatActivity implements SummaryFragment.Images {
public DataUser userData;
//region Variables
Button go;
WizardOne wizOne;
WizardTwo wizTwo;
WizardThree wizThree;
SummaryFragment summaryFragment;
Bitmap frontBitmap, backBitmap;
SharedPreferences wizardPrefs;
Bundle bundle = new Bundle();
String pathOne, pathTwo;
int currentPage = 1;
public static final String FRONT_BITMAP_KEY = "frontBitmap", BACK_BITMAP_KEY = "backBitmap";
boolean isProfileReady;
//endregion
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
userData = new DatiUtente();
isProfileReady = getIntent().getBooleanExtra("profileReady", false);
Toast.makeText(this, "profileReady : " + isProfileReady, Toast.LENGTH_SHORT).show();
setContentView(R.layout.wizard_layout);
go = (Button) findViewById(R.id.go);
if (isProfileReady) {
currentPage = 4;
go.setText("Home");
wizardPrefs = this.getSharedPreferences("USERDATA", MODE_PRIVATE);
summaryFragment = new SummaryFragment();
summaryFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.activity_layout, summaryFragment).commit();
} else {
wizOne = new WizardOne(userData);
if (savedInstanceState == null)
getSupportFragmentManager().beginTransaction().add(R.id.activity_layout, wizOne).commit();
}
go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isProfileReady) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
} else {
nextFragment();
}
}
});
}
public void nextFragment() {
switch (currentPage) {
case 1:
if (wizOne.validate()) {
wizTwo = new WizardTwo();
changeFragment(wizTwo, R.id.activity_layout);
currentPage++;
} else {
Toast.makeText(getApplicationContext(), "Riempire i campi", Toast.LENGTH_SHORT).show();
}
break;
case 2:
if (wizTwo.validate()) {
wizThree = new WizardThree(getApplicationContext());
changeFragment(wizThree, R.id.activity_layout);
currentPage++;
} else {
Toast.makeText(getApplicationContext(), "Riempire i campi", Toast.LENGTH_SHORT).show();
}
break;
case 3:
if (wizThree.validate()) {
bundle.putParcelable("frontBitmap", wizThree.getFrontBitmap());
bundle.putParcelable("backBitmap", wizThree.getBackBitmap());
summaryFragment = new SummaryFragment();
summaryFragment.setArguments(bundle);
go.setText("Home");
changeFragment(summaryFragment, R.id.activity_layout);
currentPage++;
}
break;
case 4:
passImages();
// setResult(RESULT_OK);
Intent intent = new Intent(this, SerializableWelcome.class);
intent.putExtra("dataUser", userData);
startActivity(intent);
// finish();
break;
default:
break;
}
}
public void changeFragment(ValidateFragment f, int resource) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(resource, f);
transaction.addToBackStack(null);
transaction.commit();
}
public Bitmap getFrontBitmap() {
return frontBitmap;
}
public Bitmap getBackBitmap() {
return backBitmap;
}
@Override
public void onBackPressed() {
if (currentPage > 1) {
currentPage--;
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
@Override
public Bundle passImages() {
bundle.getParcelable("frontBitmap");
bundle.getParcelable("backBitmap");
return bundle;
}
}
SerializableWelcome.java
public class SerializableWelcome extends AppCompatActivity {
TextView showName;
DataUser userData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_services_main);
showName = (TextView) findViewById(R.id.welcomeFromSummary);
Intent intent = getIntent();
if (intent != null) {
userData = (DataUser) intent.getSerializableExtra("dataUser");
}
showName.setText("Welcome " + userData.getStrFName() + " " + userData.getStrLName() + "!");
}
}
DataUser.java
public class DataUser implements Parcelable {
String strFName, strLName, strFisCode, strBirth, strCity, strZip, strProv, strCC, strIban, strMail;
public DataUser(){}
protected DataUser(Parcel in) {
//region in.readString()
strFName = in.readString();
strLName = in.readString();
strFisCode = in.readString();
strBirth = in.readString();
strCity = in.readString();
strZip = in.readString();
strProv = in.readString();
strCC = in.readString();
strIban = in.readString();
strMail = in.readString();
//endregion
}
public static final Creator<DataUser> CREATOR = new Creator<DataUser>() {
@Override
public DataUser createFromParcel(Parcel in) {
return new DataUser(in);
}
@Override
public DataUser[] newArray(int size) {
return new DataUser[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//region dest.writeString()
dest.writeString(strFName);
dest.writeString(strLName);
dest.writeString(strFisCode);
dest.writeString(strBirth);
dest.writeString(strCity);
dest.writeString(strZip);
dest.writeString(strProv);
dest.writeString(strCC);
dest.writeString(strIban);
dest.writeString(strMail);
//endregion
}
//region Getter and Setter
public String getStrFName() {
return strFName;
}
public void setStrFName(String strFName) {
this.strFName = strFName;
}
public String getStrLName() {
return strLName;
}
public void setStrLName(String strLName) {
this.strLName = strLName;
}
public String getStrFisCode() {
return strFisCode;
}
public void setStrFisCode(String strFisCode) {
this.strFisCode = strFisCode;
}
public String getStrBirth() {
return strBirth;
}
public void setStrBirth(String strBirth) {
this.strBirth = strBirth;
}
public String getStrCity() {
return strCity;
}
public void setStrCity(String strCity) {
this.strCity = strCity;
}
public String getStrZip() {
return strZip;
}
public void setStrZip(String strZip) {
this.strZip = strZip;
}
public String getStrProv() {
return strProv;
}
public void setStrProv(String strProv) {
this.strProv = strProv;
}
public String getStrCC() {
return strCC;
}
public void setStrCC(String strCC) {
this.strCC = strCC;
}
public String getStrIban() {
return strIban;
}
public void setStrIban(String strIban) {
this.strIban = strIban;
}
public String getStrMail() {
return strMail;
}
public void setStrMail(String strMail) {
this.strMail = strMail;
}
//endregion
}
'DataUser'不是'Serializable'但... – Selvin
'getparcelableextra' – Spartan
@Spartan謝謝! :) 我想這是一個愚蠢的問題從一開始.. D: – FrensisR