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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值