Adding TextInputLayout dynamically throws Exception

喜欢而已 提交于 2019-12-06 14:29:20

问题


I have been trying to add a TextInputLayout dynamically. Whenever a RadioButton will be clicked, a TextInputLayout will be added under a LinearLayout.

But after all this, I am getting an exception --

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.

Although the app is not crashing, but the code below the point of this error is not getting executed.

I have searched all over StackOverflow and other websites as well, looking for a solution, but whatever was mentioned there looks like I already have done it.

Here goes what I have tried so far --

MainActivity:

@Override
TextInputLayout textInputLayout3;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //some code here

    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);

    try{
        textInputLayout3 = new TextInputLayout(getApplicationContext());
        textInputLayout3.setLayoutParams(layoutParams);
        editText3 = new EditText(getApplicationContext());
        editText3.setLayoutParams(layoutParams);
        editText3.setHint("Search by Rating:");

        textInputLayout3.addView(editText3);

        radio3 = (RadioButton)findViewById(R.id.radio3);

        Layout3 = (LinearLayout) findViewById(R.id.ll3);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if(checkedId == radio1.getId()) {
                    //again some code here
                }

                if(checkedId == radio2.getId()) {
                    //again some code here
                }

                if(checkedId == radio3.getId()) {
                    Layout3.addView(textInputLayout3);
                }
            }
        });
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

Application(app) build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
}

Manifest XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="saubhattacharya.learningappone.com">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/learningapponeicon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Styles XML:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

Looks like everything is in place, but I am still facing this weird issue.

Can anybody please help me to identify what went wrong? Thanks in advance!


回答1:


Don't use your app context to create styled views because styling doesn't work with this context.

new TextInputLayout(getApplicationContext());

you should use your activity context

new TextInputLayout(this);


来源:https://stackoverflow.com/questions/36292766/adding-textinputlayout-dynamically-throws-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!