您的位置:首页 > 移动开发 > Android开发

总结一些小细节 ---- 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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: