关于android多个activity共享一个service中的socket问题 首先要确保Service里面的 socket真的已经连上了,然后才能获取流 否则就是空流 报NULL异常。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//这个 ImageView imageview = (ImageView) this.findViewById(R.id.imageview); Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.picture); imageview.setImageBitmap(bm); Intent intent = new Intent(this, SocketService.class); startService(intent); init(); // setContentView(R.layout.main); //这个要放在最上面。 } Socket连接也是需要时间的,在没连上的这段时间内调用它就会报空指针异常。最近也在研究Socket,还发现在安卓新的一些版本上(具体是从4.几开始我就不确定, 因为测试机是4.4的),如果在onCreate中实例Socket的话,会报一个“NetworkOnMainThreadException”,如果将网络请求放在线程UI中,安卓为了防止网络请求时间过长而造成的界面假死的情况而设计的异常。所以,在用Socket的时候建议最好新开一个线程执行。 求科普:请问Android导出应用时要创建立的“keystoe”是什么东西? 手机自己发送的信号能不能自己接收到 android如何实现重绘 android jni中使用C++ STL库 andriod可展开列表点击? 关于双击home键启动talkback服务? AndRoid android请求数据库的问题 请问诸位大神,Android怎么实现图片转动 菜鸟问题 怎么在eclipse中导入安卓源码 Notification跳转问题
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//这个
ImageView imageview = (ImageView) this.findViewById(R.id.imageview);
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.picture);
imageview.setImageBitmap(bm);
Intent intent = new Intent(this, SocketService.class);
startService(intent);
init();
// setContentView(R.layout.main); //这个要放在最上面。
}