2014年2月19日 星期三

[程式] Android - Problems & Solutions PART2

  • [ P ]
    如何開啟、使用 ADB (Android Debug Bridge) 測試 Intent Filter
    [ S ]
    a. 開啟 cmd 利用 cd 指令移動至 adb tool 的資料夾所在,若為下載 ADT bundle for Windows (http://developer.android.com/sdk/index.html) ,該資料夾位於解壓縮檔案中 "...\adt-bundle-windows-x86_64-20131030\sdk\platform-tools"
    b. 連接手機 (需事先安裝driver) 並開啟偵錯模式,並利用指令 "adb devices" 確認能正確讀取該 device
    c. 利用指令 "adb shell am start -a <YOUR_INTENT>" 測試 Intent Filter 的設定是否正確運行,其中 <YOUR_INTENT> 以 ACTION_EDIT 為例,應為 "android.intent.action.EDIT"

  • [ P ]
    如何使用 ADT 在 Project 中的 src create Fragment
    (ie. public class <Fragment Name> extends Fragment)
    [ S ]
    確定完成 Support Library Setup 後,在 ADT 中於所在 package 按右鍵選擇 New -> Class,在 Superclass 點選 Browse...,於上方輸入 "Fragment" 後在下方選取,完成其他設定後即可。

  • [ P ]
    使用 Fragment 時無法正確開啟 APP,LogCat 錯誤訊息為
    "java.lang.RuntimeException: Unable to start activity ComponentInfo{sowhat.practice.fragment/sowhat.practice.fragment.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment"
    [ S ]
    將 public class MainActivity extends Activity 改為 extends FragmentActivity

  • [ P ]
    利用 layout-large 對大尺寸 Device 做特別 layout且使用 Fragment 時無法正確開啟 APP,LogCat 錯誤訊息為
    java.lang.RuntimeException: Unable to start activity ComponentInfo{sowhat.practice.fragment/sowhat.practice.fragment.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001
    查看R.java檔後發現該 ID 為 setContentView(R.layout.home_layout); 中的 home_layout
    [ S ]
    確認 layout-large 和 layout 兩個資料夾都存在該 xml 檔 (上述例子中為 home_layout.xml),否則 run 在小尺寸 Device 時,會因 layout 資料夾中找不到檔案而出現該錯誤。

  • [ P ]
    當利用 Add fragment at runtime 的方式,欲在此 Activity 的 onclick 設定該 fragment 中 button onclick 後的動作時,出現 nullPointerException。
    [ S ]
    === Method 1 ===
    將該 onClickListener 設在該 fragment 的 class 中 onCreateView。
    ex.
    View view = inflater.inflate(R.layout.first_view, container, false);
    Button buttonReplace = (Button) view.findViewById(R.id.button_replace);
    buttonReplace.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                    // Perform action on click
            }
    });
    return view;
    但當欲執行的動作包含 replace fragment 而需使用到 "FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();",會造成 error "The method getSupportFragmentManager() is undefined for the type new View.OnClickListener(){}"。經嘗試後,改用"FragmentTransaction transaction = getFragmentManager().beginTransaction();"

    === Method 2 ===

    在 XML layout file 中設定該 button 的屬性 android:onClick="<method name>",並於此 Activity class 加入此 method。
    ex.
    public class <Activity name> extends FragmentActivity {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                    ...
            }

            public void onReplaceClicked(View view){
                     // Perform action on click
            }
    }

  • [ P ]
    變更 project 所在資料夾位置後,開啟 Eclipse 出現錯誤訊息 "The project description file (.project) is missing"
    [ S ]
    Step 1 > Delete File "workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/PROJECTNAME/.location"
    Step 2 > Reimport
    Import project to Eclipse by "New" button -> Android/Android Project from Existing Code ...
    REF: http://stackoverflow.com/questions/1531532/the-project-description-file-project-for-my-project-is-missing

  • [ P ]
    如何修改eclipse workspace預設路徑
    [ S ]
    Edit "\eclipse\configuration\.settings\org.eclipse.ui.ide.prefs"
    === Method 1 ===
    修改 RECENT_WORKSPACES 的值
    === Method 2 ===
    將 SHOW_WORKSPACE_SELECTION_DIALOG的值改為 true
    Eclipse 將會在開啟時詢問 workspace 路徑
    (設定完後記得設回 false,否則會在每次開啟 Eclipse 時都跳出詢問視窗)
    REF: http://blog.xuite.net/s900aaaa/Program/47651427-%E4%BF%AE%E6%94%B9eclipse%E8%A3%A1workspace%E9%A0%90%E8%A8%AD%E7%9A%84%E8%B7%AF%E5%BE%91

  • [ P ]
    使用 Activity extend ListActivity 時出現 LogCat 錯誤訊息 Your content must have a ListView whose id attribute is 'android.R.id.list'
    [ S ]
    將 layout file 中 ListView 的 android:id = "@+id/list" 改為 android:id = "@android:id/list"

  • [ P ]
    使用 SimpleCursorAdapter 時出現 LogCat 錯誤訊息 java.lang.IllegalArgumentException: column '_id' does not exist
    [ S ]
    將 rawQuery 的 SQL statement 中加上 rowid _id。
    ex.
    qCursor = db.rawQuery("SELECT * FROM " + DB_TABLE, null);
    => qCursor = db.rawQuery("SELECT rowid _id,* FROM " + DB_TABLE, null);
    REF: http://stackoverflow.com/questions/3359414/android-column-id-does-not-exist

沒有留言:

張貼留言