zoukankan      html  css  js  c++  java
  • Android intent 传递复杂结构的ArrayList

    YY的,从昨天晚上就搞到现在才解决这个问题,哈哈

    我希望在activity和service之间传递一个很复杂的arraylist(工作代码不好贴出来,就举个例子吧)

    List<School> scList = new ArrayList();

    我在activity:

    public class MainActivity extends Activity {
    
    
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		startHello();
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
        class School implements Serializable{
    	    String name;
    	    List<Ban> banList = new ArrayList();
        }
        
        class Ban implements Serializable{
    	    List<String>  nameList = new ArrayList() ;
    	    List<String> dspList = new ArrayList();
        }
    	
    	public void startHello(){
    		Log.i("hello","startHello");
    		Intent it = new Intent(this,HelloService.class);
    		Ban ban = new Ban();
    		School sc = new School();
    		
    		List<School> scList = new ArrayList();
    		List<Year> yList = new ArrayList();
    		List<Ban> banList = new ArrayList();
    		
    		ban.nameList.add("xiaohua");
    		ban.nameList.add("xiaocao");
    		ban.dspList.add("xioahua shi ben dan");
    		ban.dspList.add("xiaocao shi zhu");
    		
    		banList.add(ban);
    		sc.banList = banList;
    		sc.name = "yulong";
    		scList.add(sc);
    		
    		Log.i("hello","stList size = " + scList.size());
    		
    		Bundle bundle = new Bundle();
    		bundle.putSerializable("test", (Serializable)scList);
    		it.putExtras(bundle);
    		
    		ArrayList<School> pscLList = (ArrayList<School>)it.getSerializableExtra("test");
    		Log.i("hello","pscLList size = " + pscLList.size());
    		
    		startService(it);
    		
    		Log.i("hello","startHello end");
    	}
    
    }
    

     接着在service中:

    import java.util.ArrayList;
    
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    public class HelloService extends Service{
    	
    
    	@Override
    	public IBinder onBind(Intent intent) {
    		// TODO Auto-generated method stub
    		intent.getStringExtra("sherry");
    		return null;
    	}
    	
    	  @Override
    	    public void onCreate() {
    		  Log.i("hello","come in service onCreate");
    	    }
    	  
    	  @Override
    	  public void onStart(Intent intent, int startId) {
    		  Log.i("hello","come in service onStart");
    		  
    		  ArrayList<School> scList = (ArrayList<School>)intent.getSerializableExtra("test");
    		  Log.i("hello","come in service onStart size = " +scList.size());
    			for(int i=0;i<scList.size();i++){
    				School sc = scList.get(i);
    				Log.i("hello","name:" + sc.name);
    			}
    			Log.i("hello","come in service onStart end");
    			
    	  }
    
    }
    

     结果报错:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testhello/com.example.testhello.MainActivity}: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.testhello.MainActivity$School)

    后面突然想要不把里面定义的几个类拿出来,哈哈,果真就对了,把School,和Ban独立出来后,Ok,

    太坑爹了。

     

  • 相关阅读:
    css浏览器兼容问题集锦
    【转】H264编码原理以及I帧B帧P帧
    Makefile Shell 脚本;sed命令
    oProfile 学习
    C++ 局部变量的析构
    【转】C++ 单例模式
    C++ operator 知识点 2
    C++ operator 知识点
    218多校第九场 HDU 6424 (数学)
    2018多校第九场 HDU 6416 (DP+前缀和优化)
  • 原文地址:https://www.cnblogs.com/mickeyontheway/p/3553141.html
Copyright © 2011-2022 走看看