总结一些小细节 ---- Android

原创 2011年01月07日 15:17:00

1.Null pointer dereference of parent.getItemAtPosition(...) where null comes from constant

 

 

This error always happened in the build of spinner ,like this code:

 

Spinner spinner = (Spinner)findViewById(R.id.selectserver);


 ArrayAdapter adapterserver = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, keyServers);
adapterserver.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterserver);

spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
                @Override
                public void onItemSelected(AdapterView<?> parent, View arg1,
                    int postion, long id) {
                    String lastServer = null;
                    selectedServerName = parent.getItemAtPosition(postion).toString();

                }

}

 

We check these code ,and always we see it is fine,without any error when we making some build.

 

But by some complict rule, it will make the bug:"Null pointer dereference of parent.getItemAtPosition(...) where null comes from constant".

And then we will be confursed about this ,why this code had this error?

 

Fixed Method:

we can make one default value to be position's default value so that it can solve this error.

And use shareperference to store the data that you choose in the view.

 

Like:

spinner.setSelection(settings.getInt("ServerNumPosition", 0));
            spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
                @Override
                public void onItemSelected(AdapterView<?> parent, View arg1,
                    int postion, long id) {

 

            }

            editorse t tings.putInt("ServerNumPosition", postion);
            editorsettings.commit();

}

**************************************************************************

 

2.start and stop one service under /system/bin/  by apk method

 

Method1:

Make the apk's sharedUserId to be system.

AndroidManifest.xml

android:sharedUserId="android.uid.system"

 

In Java code:

import android.os.SystemProperties;

SystemProperties.set("ctl.start", "eCompassd");    // start the /system/bin/eCompassd

SystemProperties.set("ctl.stop", "eCompassd");    // stop the /system/bin/eCompassd

 

())()()()()()()()()()()()()()()()()()()()()()()()()()()()()

or use

Runtime.getRuntime().exec("/system/bin/eCompassd");    // stop the /system/bin/eCompassd

 

 

Method 2:

connect the socket with 127.0.0.1

and send command to service ,to stop it.

 

3. if you want to change the permisson of data directory in you apk application

 

you can use this:

Make you apk's sharedUserId to system,and Run command:

 

Runtime.getRuntime().exec("chmod 777 " + /data/);

 

4.in the init.rc file ,when you want to start one service from apk,then you can make this service to be "oneshot"

delay time in java ,you can use Thread.sleep(2222);

 

 

 

相关文章推荐

【Cocos2dx通信(Http&Socket)相关编译到Android细节总结】编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i

本站文章均为 李华明Himi 原创,转载务必在明显处注明:(作者新浪微博: @李华明Himi ) 转载自【黑米GameDev街区】 原文链接: http://www.himigame....

Android细节问题总结(一)

这篇博客是用来记录自己在写代码的过程中遇到的一些问题,以及解决方法,做一个总结,算是笔记吧。...

Cocos2dx通信(Http&Socket)相关编译到Android细节总结 编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_in

本篇介绍在Cocos2dx中加入网络通信相关代码,然后编译到Android时出现的一些细节和需要注意的地方总结。不多废话了,直接进入正题;    1.  首先介绍在Cocos2dx中使用pthr...

Android细节问题总结(二)

这篇博客是用来记录自己在写代码的过程中遇到的一些问题,以及解决方法,做一个总结,算是笔记吧。...

Android Studio使用细节总结

Android Studio使用总结积累问题解决

【Cocos2dx通信(Http&Socket)相关编译到Android细节总结】编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i

本站文章均为 李华明Himi 原创,转载务必在明显处注明:转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/android-game/881.html   ...

android视频监控实例技术细节略解

  • 2013年09月04日 18:35
  • 593KB
  • 下载

编写SQL语句的细节总结

  • 2008年12月11日 04:44
  • 804KB
  • 下载

Android系统Recovery工作原理之使用update.zip升级过程分析(六)---Recovery服务流程细节

Recovery服务毫无疑问是Recovery启动模式中最核心的部分。它完成Recovery模式所有的工作。Recovery程序对应的源码文件位于:/gingerbread0919/bootable/...

struts2.x一些技术细节资料总结

  • 2010年01月06日 16:10
  • 20KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:总结一些小细节 ---- Android
举报原因:
原因补充:

(最多只允许输入30个字)