关闭

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

1133人阅读 评论(0) 收藏 举报

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);

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:58715次
    • 积分:1042
    • 等级:
    • 排名:千里之外
    • 原创:46篇
    • 转载:6篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论