почему eclipse показывает ошибку, приложение неожиданно остановилось. Пожалуйста, повторите попытку в классе InputMethodManager?

#java #android #eclipse

#java #Android #eclipse

Вопрос:

У меня проблема с моим приложением для Android. Я хочу запустить проект с классом InputMethodManager, но Eclipse показывает мне ошибку, приложение неожиданно остановилось.

Я не использую никаких методов, только создаю экземпляр InputMethodManager. Я не знаю, что не так.

Мой код:

 private static InputMethodManager inputMethodManager;

private static EditText editText;

private static String stringInstance;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_method_manager);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

}
 

Мой журнал ошибок:

 06-27 16:03:11.631: E/PhonePolicy(32): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
06-27 16:03:26.411: E/BatteryService(80): usbOnlinePath not found
06-27 16:03:26.411: E/BatteryService(80): batteryVoltagePath not found
06-27 16:03:26.411: E/BatteryService(80): batteryTemperaturePath not found
06-27 16:03:30.851: E/EventHub(80): could not get driver version for /dev/input/mouse0, Not a typewriter
06-27 16:03:30.851: E/EventHub(80): could not get driver version for /dev/input/mice, Not a typewriter
06-27 16:03:31.082: E/WifiService(80): Invoking mWifiStateMachine.setWifiEnabled
06-27 16:03:32.541: E/ThrottleService(80): Could not open GPS configuration file /etc/gps.conf
06-27 16:03:32.551: E/NetworkTimeUpdateService(80): Could not open GPS configuration file /etc/gps.conf
06-27 16:03:32.551: E/NetworkTimeUpdateService(80): NTP server address not found, not syncing to NTP time
06-27 16:03:33.342: E/logwrapper(161): executing /system/bin/tc failed: No such file or directory
06-27 16:03:33.421: E/logwrapper(162): executing /system/bin/tc failed: No such file or directory
06-27 16:03:33.451: E/logwrapper(163): executing /system/bin/tc failed: No such file or directory
06-27 16:03:43.192: E/SoundPool(80): error loading /system/media/audio/ui/Effect_Tick.ogg
06-27 16:03:43.192: E/SoundPool(80): error loading /system/media/audio/ui/Effect_Tick.ogg
06-27 16:03:43.202: E/SoundPool(80): error loading /system/media/audio/ui/Effect_Tick.ogg
06-27 16:03:43.202: E/SoundPool(80): error loading /system/media/audio/ui/Effect_Tick.ogg
06-27 16:03:43.212: E/SoundPool(80): error loading /system/media/audio/ui/Effect_Tick.ogg
06-27 16:03:43.262: E/SoundPool(80): error loading /system/media/audio/ui/KeypressStandard.ogg
06-27 16:03:43.272: E/SoundPool(80): error loading /system/media/audio/ui/KeypressSpacebar.ogg
06-27 16:03:43.322: E/SoundPool(80): error loading /system/media/audio/ui/KeypressDelete.ogg
06-27 16:03:43.332: E/SoundPool(80): error loading /system/media/audio/ui/KeypressReturn.ogg
06-27 16:03:43.542: E/TelephonyManager(80): Hidden constructor called more than once per process!
06-27 16:03:43.542: E/TelephonyManager(80): Original: android, new: android
06-27 16:03:59.692: E/AndroidRuntime(378): FATAL EXCEPTION: main
06-27 16:03:59.692: E/AndroidRuntime(378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imm/com.example.imm.InputMethodManager}: java.lang.ClassCastException: android.view.inputmethod.InputMethodManager cannot be cast to com.example.imm.InputMethodManager
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.os.Looper.loop(Looper.java:132)
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.app.ActivityThread.main(ActivityThread.java:4025)
06-27 16:03:59.692: E/AndroidRuntime(378):  at java.lang.reflect.Method.invokeNative(Native Method)
06-27 16:03:59.692: E/AndroidRuntime(378):  at java.lang.reflect.Method.invoke(Method.java:491)
06-27 16:03:59.692: E/AndroidRuntime(378):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-27 16:03:59.692: E/AndroidRuntime(378):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-27 16:03:59.692: E/AndroidRuntime(378):  at dalvik.system.NativeStart.main(Native Method)
06-27 16:03:59.692: E/AndroidRuntime(378): Caused by: java.lang.ClassCastException: android.view.inputmethod.InputMethodManager cannot be cast to com.example.imm.InputMethodManager
06-27 16:03:59.692: E/AndroidRuntime(378):  at com.example.imm.InputMethodManager.onCreate(InputMethodManager.java:35)
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
06-27 16:03:59.692: E/AndroidRuntime(378):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
06-27 16:03:59.692: E/AndroidRuntime(378):  ... 11 more
: E/(): Device disconnected
 

Ответ №1:

Вы импортировали неправильный класс InputMethod Manager, посмотрите на ваше исключение :

Unable to start activity ComponentInfo{com.example.imm/com.example.imm.InputMethodManager}: java.lang.ClassCastException: android.view.inputmethod.InputMethodManager cannot be cast to com.example.imm.InputMethodManager

Просто измените значение import com.example.imm.InputMethodManager; на import android.view.inputmethod.InputMethodManager; в верхней части вашего java-файла.

Комментарии:

1. Если я импортирую android.view.inputmethod. InputMethodManager Eclipse просмотрите 2 опции рядом с этим импортом: удалить неиспользуемый импорт и упорядочить импорт.