1

I am trying to get a Custom keyboardView from layout a file. Here is the code

public class MyInputMethodService extends InputMethodService
        implements KeyboardView.OnKeyboardActionListener {

...
    @Override 
    public View onCreateInputView() {

        SindhikKeyboardView mInputView = (SindhikKeyboardView) getLayoutInflater().inflate(
                                      R.layout.keyboard, null).findViewById(R.id.keyboard);
        mInputView.setOnKeyboardActionListener(this);
        setLatinKeyboard(mQwertyKeyboard);
        return mInputView;
    }
...
}

But it is giving me an error

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

The code in the layout file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<io.github.fahadmaqsood.sindhik.SindhikKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/keyboard" />

</RelativeLayout>

I know that .findViewById() is causing the problem. If I remove that and the RelativeLayout element from the layout file, It works! but I want that RelativeLayout to be there... how can I get rid of that error? There are answers similar to the error I got, on this site. but I can't figure out a solution to my problem through them.

Edit: Full error log

E/AndroidRuntime: FATAL EXCEPTION: main Process: io.github.fahadmaqsood.sindhik, PID: 3126 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:3562) at android.view.ViewGroup.addView(ViewGroup.java:3415) at android.view.ViewGroup.addView(ViewGroup.java:3391) at android.inputmethodservice.InputMethodService.setInputView(InputMethodService.java:1260) at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1117) at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1471) at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1446) at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:455) at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:202) at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) at dalvik.system.NativeStart.main(Native Method)

  • 1
    can you post more of logcat and highlight the lines where actually the error is – Abhinav Chauhan Mar 03 '20 at 11:19
  • @TheGoldy I have added now! – Fahad Maqsood Qazi Mar 03 '20 at 16:13
  • https://stackoverflow.com/questions/28071349/the-specified-child-already-has-a-parent-you-must-call-removeview-on-the-chil let me know if you find answer on that link – Abhinav Chauhan Mar 04 '20 at 03:52
  • No, it didn't help. and no matter where ever I use removeView() it always says "the method removeView() is not resolved." and I think my problem is different than the one in that question. I am not appending anything I am just getting something through findViewById() – Fahad Maqsood Qazi Mar 04 '20 at 12:21

0 Answers0