我目前正在使用Serializable Interface。我想将ArrayList从一个活动传递到另一个活动,但它给了我这个错误“无法编组值”,代码是,
//发送活动:
public class LogIn extends Activity implements OnClickListener
{
ProgressDialog pd;
ImageButton logIn,registr;
EditText userName,Password;
public static String usrName,passwrd;
String TAG=LogIn.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
userName=(EditText)findViewById(R.id.logIn_etuserName);
Password=(EditText)findViewById(R.id.logIn_etpaswrd);
logIn=(ImageButton)findViewById(R.id.logIn_B_logIn);
registr=(ImageButton)findViewById(R.id.logIn_B_registration);
logIn.setOnClickListener(this);
registr.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
String respns="";
switch (v.getId())
{
case R.id.logIn_B_logIn:
try
{
usrName=userName.getText().toString();
passwrd=Password.getText().toString();
if(usrName.contentEquals("") && passwrd.contentEquals(""))
{
Toast.makeText(getApplicationContext(), "Enter userName",Toast.LENGTH_LONG).show();
}
// else if(passwrd.contentEquals(""))
// {
// Toast.makeText(getApplicationContext(), "Enter password",Toast.LENGTH_LONG).show();
// }
else
{
pd=ProgressDialog.show(LogIn.this, "LogIn", "Logging");
new Athenticate().execute();
}
} catch (Exception e)
{
e.printStackTrace();
}
break;
case R.id.logIn_B_registration:
Intent i=new Intent(LogIn.this,Registration.class);
startActivity(i);
break;
default:
break;
}
}
private class Athenticate extends AsyncTask<String, Void,ArrayList<SettrGettr> >
{
ArrayList<SettrGettr> lst;
@Override
protected ArrayList<SettrGettr> doInBackground(String... params)
{
lst=(ArrayList<SettrGettr>)new AllMethods().logInMethod();
pd.cancel();
return lst;
}
@Override
protected void onPostExecute(ArrayList<SettrGettr> result)
{
ArrayList<SettrGettr> l=result;
try {
if(l.size()>0)
{
Intent i=new Intent(LogIn.this,Home.class);
Bundle b=new Bundle();
b.putSerializable("shahid",l);
//i.putExtras(b);
i.putExtras(b);
startActivity(i);
}
else
{
}
} catch (Exception e)
{
Log.d(TAG, "Errors are ::"+e.getMessage());
}
}
}
}
//Recieving Activity:
public class Home extends Activity implements Serializable
{
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
lv=(ListView)findViewById(R.id.home_lv);
ArrayList<SettrGettr> gtList=(ArrayList<SettrGettr>)getIntent().getSerializableExtra("shahid");
//(ArrayList<SettrGettr>)getIntent().getSerializableExtra("shahid");
ArrayAdapter<SettrGettr> adp=new ArrayAdapter<SettrGettr>(Home.this,android.R.layout.simple_list_item_1,gtList);
lv.setAdapter(adp);
}
}
//setterGettr:
public class SettrGettr {
int ID;
String Name = "";
String FName = "";
String Class = "";
String UserName = "";
String Password = "";
String DOB = "";
String MobileNo = "";
String Relation = "";
ArrayList<SettrGettr> msttrList;
public ArrayList<SettrGettr> getMsttrList() {
return msttrList;
}
public void setMsttrList(ArrayList<SettrGettr> msttrList) {
this.msttrList = msttrList;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getFName() {
return FName;
}
public void setFName(String fName) {
FName = fName;
}
public String getCurrentClass() {
return Class;
}
public void setClass(String class1) {
Class = class1;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getDOB() {
return DOB;
}
public void setDOB(String dOB) {
DOB = dOB;
}
public String getMobileNo() {
return MobileNo;
}
public void setMobileNo(String mobileNo) {
MobileNo = mobileNo;
}
public String getRelation() {
return Relation;
}
public void setRelation(String relation) {
Relation = relation;
}
@Override
public String toString() {
return "\nRegistration No\t: " + getID() + "\n\nName\t\t\t\t\t\t\t: " + getName() + "\n\nFather Name\t\t\t: " + getFName() + "\n\nClass\t\t\t\t\t\t\t: "
+ getCurrentClass() + "\n\nCity\t\t\t\t\t\t\t\t: Islamabad" + "\n\nAdress\t\t\t\t\t\t: Islamabad";
}
}
答案 0 :(得分:9)
做一件事而不是尝试
public class Home extends Activity implements Serializable
从上方删除implements Serializable
并添加
public class SettrGettr implements Serializable{
int ID;
String Name = "";
String FName = "";
String Class = "";
String UserName = "";
String Password = "";
String DOB = "";
String MobileNo = "";
String Relation = "";
ArrayList<SettrGettr> msttrList;
public ArrayList<SettrGettr> getMsttrList() {
return msttrList;
}
public void setMsttrList(ArrayList<SettrGettr> msttrList) {
this.msttrList = msttrList;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getFName() {
return FName;
}
public void setFName(String fName) {
FName = fName;
}
public String getCurrentClass() {
return Class;
}
public void setClass(String class1) {
Class = class1;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getDOB() {
return DOB;
}
public void setDOB(String dOB) {
DOB = dOB;
}
public String getMobileNo() {
return MobileNo;
}
public void setMobileNo(String mobileNo) {
MobileNo = mobileNo;
}
public String getRelation() {
return Relation;
}
public void setRelation(String relation) {
Relation = relation;
}
@Override
public String toString() {
return "\nRegistration No\t: " + getID() + "\n\nName\t\t\t\t\t\t\t: " + getName() + "\n\nFather Name\t\t\t: " + getFName() + "\n\nClass\t\t\t\t\t\t\t: "
+ getCurrentClass() + "\n\nCity\t\t\t\t\t\t\t\t: Islamabad" + "\n\nAdress\t\t\t\t\t\t: Islamabad";
}
}
<强>解释强>
当您通过bundle传递可序列化的内容时,您传递的值必须是Implements Serializable interface。
但是你可以从传递值的地方实现Serializable接口。