总结一些小细节 ---- Android
2011-01-07 15:17
435 查看
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);
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);
相关文章推荐
- Android 开发中的遇到的一些细节问题总结
- android nuplayer做的一些细节优化相关总结
- 我在工作中总结的一些前端常用小细节 (转载)
- 对Android Handler Message Looper常见用法,知识点的一些总结
- Android 自定义View属性相关细节总结
- C语言的一些细节总结
- 一些细节,需要注意;一些小错,难以察觉(Android SharedPreferences、Thread、Toast、AsyncTask)
- 面向对象__阶段总结__【覆盖与重载的区别】【继承,多态的一些细节】
- 【Cocos2dx通信(Http&Socket)相关编译到Android细节总结】编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i
- 总结一些 android 平常用到的自定义控件
- Android中Surface和SurfaceView的一些理解和总结
- Android的一些总结
- Android Webview的一些使用总结和遇到过得坑
- android之开发总结,常见问题,细节问题,用户体验
- Android媒体的一些使用总结
- android EditText 软键盘 一些总结
- [转]Android媒体的一些使用总结
- 在Android中使用adb命令时关于权限方面的一些总结
- 关于使用Delphi XE10 进行android开发的一些总结
- 总结一些 android 平常用到的自定义控件