Android 运行时错误:无法封送值

Posted

技术标签:

【中文标题】Android 运行时错误:无法封送值【英文标题】:Android Runtime Error :Unable to marshal value 【发布时间】:2014-06-26 19:02:21 【问题描述】:

我这次使用的是可序列化接口。我想将 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";

    


【问题讨论】:

【参考方案1】:

做一件事,然后尝试

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 传递可序列化的东西时,你传递的值必须实现 Serializable 接口。

但是您需要从传递值的地方实现 Serializable 接口。

【讨论】:

我将值传递给已经实现 Serializable 接口的 Home 类

以上是关于Android 运行时错误:无法封送值的主要内容,如果未能解决你的问题,请参考以下文章

出现错误 - 运行本机反应时无法识别的命令“run-android”,

C# 结构到字节数组封送产生 19 个元素而不是 20 个

在android平台类路径错误中运行任何颤振项目时出错

如何在运行应用程序时修复 android studio 版本 3.6.2 中的“无法解析结果路径字符串:”错误?

无法访问包管理器。安装android应用程序时系统是不是正在运行

无法访问包管理器。安装android应用程序时系统是不是正在运行