1. 程式人生 > >總結一些小細節 ---- Android

總結一些小細節 ---- 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);

相關推薦

總結一些細節 ---- Android

1.Null pointer dereference of parent.getItemAtPosition(...) where null comes from constant This error always happened in the build of sp

spring中set註入的一些細節錯誤

學習 細節 borde 集合 south let odi 桂林 代碼 這是小白偶爾一直null指針的錯誤,調試了好久,原來是自己對spring註入的不夠了解 我相信有很多跟我差不多的初學者會遇上,所以特地寫出來,防止有人跟我一樣。哈哈,也寫上去,以防自己下次還犯這樣的錯誤。

JavaScriput的一些細節

cond bsp demo ren setattr 內置 內置對象 h+ 生成 創建一個節點 window.onload=function(){ var demo=document.getElementById("demo"); //獲取第一個按鈕索引為0 var btn=

【UE4實用技能】UE4藍圖開發中的一些細節

加載圖標 com ges 路徑 icons 就是 技能 9.png tab 1.加載圖標Icon路徑:/Game/Arts/UI/TableIcons/Task_Icon/Achievement/Task_icon_weixin.Task_icon_weixin(就是Con

使用SKlearn中的SpectralClustering 中的一些細節

在使用SKlearn中的聚類演算法時需要引入 cluster 模組,使用譜聚類時(或者其他任意一個聚類演算法,如KMeans)都需要先進行聚類演算法的一些必要的引數設定,這裡舉例的譜聚類就需要配置要聚類的簇數。即n_clusters,n_neighbors,eigensolve等引數雖然都有預設設定

QML TreeView的樣式和使用時的一些細節

TreeView目前只在 Controls 1.4中有,所以在使用的時候要先匯入。   關於TreeView的Item和Model,這個在網上搜能搜到很多,同時這個在Qt Creator的歡迎介面裡輸入TreeView,選擇第一個專案,之後進入專案說明往下拉就能看到。 &nbs

Go語言-型別中的一些細節

把平時的一些筆記放上來,主要是一些比較小的問題,這裡是關於golang中的型別中需要記錄的小知識點。 常量 【1】可以是某些編譯器能計算出結果的表示式 unsafe.Sizeof len cap const { ptrSize = unsaf

html標籤中的一些細節

有序列表:    type的作用:可以根據type做選擇前期的符號為數字 還是 a A字母 還是I符號   reversed ="reversed"  倒著排序    start=“” 做判斷從第幾個開始排序 無序列表  type值disc 為預設黑點

vue2總結一些功能

一 、keep-alive 實現router切換狀態保留 如果載入的資料ok 就不用重複載入了。程式碼示例 123456789101112131415 <template> <div class="tab"> <rou

JSE基礎總結1---細節

dos常用命令列:     dir:列出當前目錄的資料夾     md:建立目錄     rd:刪除目錄     cd:進入指定目錄     cd

寫程式碼的一些細節,養成好習慣

1.如果一個成員變數(基本型別的)在某個方法中引用較多,則在方法中定義一個區域性變數。這樣引用效率就高了,但是如果引用的是物件成員,那就在定義區域性變數也沒有用了,因為物件並不可以儲存在棧中。 2.如果一個變數是在接下來的使用中是不改變的,那麼就用final去修飾。

scanf的一些細節

格式字串 格式字串的一般形式為:    %[*][輸入資料寬度][長度]型別其中有方括號[]的項為任選項。各項的意義如下。1) 型別表示輸入資料的型別,其格式符和意義如下表所示。 格式 字元意義 d 輸入十進位制整數 o 輸入八進位制整數 x 輸入十六進位制整數 u 輸

Maven的一些細節(包含SSM整合)

更改倉庫地址 進入: C:\Users\Bam\Desktop\apache-maven-3.5.3-bin\apache-maven-3.5.3\conf 增加國內倉庫映象 阿里雲 <mirror> &

mysql中關於order by的一些細節

order by我們都知道是用於排序的 今天看到一些很奇妙的寫法 order by --id order by null 以前沒見過這東西,老稀奇了。興致來了,就做個實驗研究一下 mysql> explain select * from test1 ; +----+-

NodeJS 的一些細節

exports 和 module.exports 的區別 require 用來載入程式碼,而 exports 和 module.exports 則用來匯出程式碼。但很多新手可能會迷惑於 exports 和 module.exports 的區別,為了更好的理解 expo

vue一些細節

1.在vue組建中,每個元件的data都是function,返回一個物件,這樣每個元件的data都是獨立的一個物件,不會相互影響。 2.$ref在vue獲取當前元素的索引,也就是dom物件,類似document。getelementbyid如果是在元件上,獲取的是這個元件,

android開發一些bug

string -m 怎麽辦 post ring oar content trac deb 1.一定要註意findViewId這種方法,尤其是含有多個同樣的R.id的名字時,debug時不會當場報錯。但隨後會報空指針錯誤 2.List轉換為Array能夠這樣處理:

C#、Java中的一些功能點總結(持續更新......)

grid datagrid item 其他 cnblogs hid roc oid sha 前言:在項目中,有時候一些小的功能點,總是容易讓人忽略,但是這些功能加在項目中往往十分的有用,因此筆者在這裏總結項目中遇到的一些實用的小功能點,以備用,並持續更新...... 1.禁

關於vue一些容易出錯的細節

per 控制臺 需要 .... 一個 出現 強調 prop 關於 當我們實現一個vue功能的時候,可能會遇到這樣的問題,表面上你的頁面運行沒有任何問題,而且功能也都能實現,但是打開控制臺的時候,會看到“ property of ‘xxx‘ undined”等.....一般解

css細節總結

ott 內容 left fault -1 isa 單元格 :hover javascrip css小細節總結 1、用max-width來防止圖片撐破容器 img{   display: inline-block;   max-width: 100%; } 2、用pointe