我开始学习java和android开发,并且因为将我的MainMenu活动中的自定义数组传递给另一个用于打印输出值的活动而陷入困境。任何人都可以指出我哪里出错了吗?
MainMenu课程:
public class MainMenu extends AppCompatActivity{
SharedPreferences sharedVar;
Controller controller = new Controller();
int size = 100;
Sati[] sati = new Sati[size];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
sharedVar = this.getSharedPreferences("Var", MODE_WORLD_READABLE);
}
@Override
public void onResume(){
super.onResume();
for(int i = 0;i<size;i++) {
if (sati[i] != null){
controller.upisiSmjenu(sati[i],sharedVar.getLong("Datum dolaska",0),sharedVar.getLong("Datum odlaska",0),sharedVar.getLong("Razlika",0));
}
}
}
public void otvoriUnosLayout(View view) {
Intent intent = new Intent(this, UnosSati.class);
startActivity(intent);
}
public void otvoriPregledLayout(View view) {
Intent intent = new Intent(this, PregledSati.class);
intent.putExtra("Sati",sati);
startActivity(intent);
}
}
UnosSati Class(用于数据输入):
public class UnosSati extends FragmentActivity {
SharedPreferences sharedVar;
TextView txtRazlika;
Sati sati;
Long razlika;
Date dateD,dateO;
int danD,mjD,godD,SD,MD,danO,mjO,godO,SO,MO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unos_sati);
sharedVar = this.getSharedPreferences("Var",MODE_WORLD_READABLE);
sati = (Sati)getIntent().getParcelableExtra("Sati");
dateD = new Date();
dateO = new Date();
txtRazlika = (TextView) findViewById(R. id. txtRazlika);
razlika = 0L;
txtRazlika.setText(String.valueOf(razlika));
danD = sharedVar.getInt("DanD",0);
mjD = sharedVar.getInt("MjD",0);
godD = sharedVar.getInt("GodD",0)-1900;
SD = sharedVar.getInt("SD",0);
MD = sharedVar.getInt("MD",0);
danO = sharedVar.getInt("DanO",0);
mjO = sharedVar.getInt("MjO",0);
godO = sharedVar.getInt("GodO",0)-1900;
SO = sharedVar.getInt("SO",0);
MO = sharedVar.getInt("MO",0);
dateD.setDate(danD);
dateD.setMonth(mjD);
dateD.setYear(godD);
dateD.setHours(SD);
dateD.setMinutes(MD);
dateO.setDate(danO);
dateO.setMonth(mjO);
dateO.setYear(godO);
dateO.setHours(SO);
dateO.setMinutes(MO);
razlika =((dateO.getTime() - dateD.getTime())/60000)/60;
txtRazlika.setText(String.valueOf(razlika));
}
public void vratiNaGlavni(View view){
sharedVar.edit().putLong("Datum dolaska", dateD.getTime()).commit();
sharedVar.edit().putLong("Datum odlaska", dateO.getTime()).commit();
sharedVar.edit().putLong("Razlika",razlika).commit();
Intent intent = new Intent(this,MainMenu.class);
startActivity(intent);
}
public void otvoriKalendarDolaskaLayout(View view) {
Intent intent = new Intent(this, Datum_dolaska.class);
startActivity(intent);
}
public void otvoriSatDolaskaLayout(View view) {
Intent intent = new Intent(this, Sat_dolaska.class);
startActivity(intent);
}
public void otvoriKalendarOdlaskaLayout(View view) {
Intent intent = new Intent(this, Datum_odlaska.class);
startActivity(intent);
}
public void otvoriSatOdlaskaLayout(View view) {
Intent intent = new Intent(this, Sat_odlaska.class);
startActivity(intent);
}
}
用于查看数据的PregledSati类:
public class PregledSati extends AppCompatActivity {
TextView textView1,textView2,textView3,textView4,textView5,textView6;
Date dateD,dateO;
String printD,printO,printR;
Sati[] sati;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pregled_sati);
dateD = new Date();
dateO = new Date();
printD = printO = printR = "";
textView1 = (TextView) findViewById(R. id. txtIspis1);
textView2 = (TextView) findViewById(R. id. txtispis2);
textView3 = (TextView) findViewById(R. id. txtispis3);
Bundle extras = getIntent().getExtras();
Sati[] sati = getIntent().getParcelableExtra("Sati");
for(int i = 0;i<100;i++){
textView1.append(String.valueOf(sati[i].getDateDolaska()));
textView1.append("\n");
}
}
}
萨蒂班:
public class Sati implements Parcelable{
public long dateDolaska, dateOdlaska,ukupnoVrijeme;
public Sati() {
super();
}
public Sati(Parcel dest) {
this.dateDolaska=dest.readLong();
this.dateOdlaska=dest.readLong();
this.ukupnoVrijeme=dest.readLong();
}
public long getDateDolaska() { return dateDolaska; }
public void setDateDolaska(long dateDolaska) {
this.dateDolaska = dateDolaska;
}
public long getDateOdlaska() {
return dateOdlaska;
}
public void setDateOdlaska(long dateOdlaska) {
this.dateOdlaska = dateOdlaska;
}
public long getUkupnoVrijeme() {
return ukupnoVrijeme;
}
public void setUkupnoVrijeme(long ukupnoVrijeme) {
this.ukupnoVrijeme = ukupnoVrijeme;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this.dateDolaska);
dest.writeLong(this.dateOdlaska);
dest.writeLong(this.ukupnoVrijeme);
}
public static final Creator<Sati> CREATOR=new Creator<Sati>() {
@Override
public Sati createFromParcel(Parcel dest) {
return new Sati(dest);
}
@Override
public Sati[] newArray(int size) {
return new Sati[size];
}
};
}
答案 0 :(得分:0)
我认为是因为你必须得到一个Parcelables数组,并确保Sati类是Parcelable。
Sati[] sati = getIntent().getParcelableArrayExtra("Sati");