在項目中需要引用settings模塊裏面的某個活動。在eclipse中導入settins.jar包之後,使用如下方式啓動:
Intent intent = new Intent();
intent.setAction("android.settings.INPUT_METHOD_SETTINGS");
intent.addCategory("android.intent.category.VOICE_LAUNCH");
intent.setClass(this, DateTimeSettingsSetupWizard.class);
startActivity(intent);
編譯並不會報錯,但是在運行的時候就提示:ClassNotFoundException錯誤。在網上找添加三方庫報錯的解決方法都不行。後來換了一種是實現方式,且不需要導入settings.jar這個庫了。
Intent intent = new Intent();
intent.setAction("android.settings.INPUT_METHOD_SETTINGS");
ComponentName c = new ComponentName("com.android.settings", "com.android.settings.LanguageSettings");
intent.setComponent(c);
intent.addCategory("android.intent.category.VOICE_LAUNCH");
startActivity(intent);
對,就是設定ComponentName的方式啓動。不需要導入包,且運行沒有錯誤。
並不清楚爲什麼導入包,會出現錯誤。有可能這個包導入軟件之後,實際上的加載,初始化都是在本應用的空間內進行。而你想要引用的類是在系統的包裏面,並不在一個空間內,導致找不到這個類。
有更好解釋或者有什麼線索的朋友,希望在評論中添加。拜謝!
參考評論中的內容,會持續更新本文。