Cannot show TabLayout in InputView

165 views Asked by At

I'm creating an Android input method, and want to use TabLayout from the support library in the InputView (the UI where the user inputs text in the form of keyclicks).

The layout for the InputView is very simple:

<?xml version="1.0" encoding="utf-8"?>
<org.szm.enigma2.ime.InputView xmlns:android="http://schemas.android.com/apk/res/android"
                               android:orientation="vertical"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content">
    <android.support.design.widget.TabLayout android:id="@+id/tab"
                                             android:layout_width="match_parent"
                                             android:layout_height="wrap_content"
                                             android:layout_gravity="center_horizontal"
                                             android:layout_alignParentTop="true"
                                             style="@style/Theme.AppCompat">
        <android.support.design.widget.TabItem android:text="Hello"/>
        <android.support.design.widget.TabItem android:text="World"/>
        <android.support.design.widget.TabItem android:text="Enigma"/>
    </android.support.design.widget.TabLayout>
</org.szm.enigma2.ime.InputView>

And I use the same layout for both InputView and the settings activity, which is a normal activity.

Create InputView from subclass of InputMethodService:

return getLayoutInflator().inflat(R.layout.input_view, null);

Inside the constructor of settings activity class:

setContentView(R.layout.input_view);

When I run the program, the settings activity is normal. Then tab layout is working perfectly. But when I activate the input method's input view, the program crashes with the following info:

09-08 18:56:49.064 28437-28437/org.szm.enigma2 E/AndroidRuntime: FATAL EXCEPTION: main
        Process: org.szm.enigma2, PID: 28437
        android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class android.support.design.widget.TabLayout
        Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class android.support.design.widget.TabLayout
        Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
            at android.view.LayoutInflater.createView(LayoutInflater.java:652)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
            at org.szm.enigma2.ime.InputService.onCreateInputView(InputService.java:42)
            at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1228)
            at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1622)
            at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1590)
            at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:442)
            at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:206)
            at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6175)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
        Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
            at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
            at android.support.design.widget.TabLayout.<init>(TabLayout.java:297)
            at android.support.design.widget.TabLayout.<init>(TabLayout.java:291)
            at java.lang.reflect.Constructor.newInstance0(Native Method) 
            at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
            at android.view.LayoutInflater.createView(LayoutInflater.java:652) 
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812) 
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:883) 
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:522) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:430) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
            at org.szm.enigma2.ime.InputService.onCreateInputView(InputService.java:42) 
            at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1228) 
            at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1622) 
            at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1590) 
            at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:442) 
            at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:206) 
            at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:154) 
            at android.app.ActivityThread.main(ActivityThread.java:6175) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
09-08 18:56:49.081 28437-28437/? I/Process: Sending signal. PID: 28437 SIG: 9

I don't know why it didn't work even if I set the style attribute. And how can I use tab layout in an input view? (since input view is not inside any activity)

Thanks

EDIT

I have already checked this question and googled other resources. Most of them is dealing with error of inflating TabLayout in Activity. My problem is that TabLayout works fine inside Activity but not in input view.

1

There are 1 answers

0
Leon On

you can do it like this:

Context context = new ContextThemeWrapper(this, android.support.v7.appcompat.R.style.Theme_AppCompat_Light_NoActionBar);
LayoutInflater inflater = LayoutInflater.from(context);
view = (View) inflater.inflate(R.layout.XXXXX, null);

the Tablayout is in the R.layout.XXXXX