【问题标题】:Adding TextInputLayout dynamically throws Exception动态添加 TextInputLayout 会抛出异常
【发布时间】:2016-03-30 03:30:11
【问题描述】:

我一直在尝试动态添加TextInputLayout。每当点击RadioButton 时,TextInputLayout 就会添加到LinearLayout 下。

但在这一切之后,我得到了一个例外——

java.lang.IllegalArgumentException:您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用。

虽然应用程序没有崩溃,但该错误点以下的代码没有被执行。

我已经搜索了StackOverflow和其他网站,寻找解决方案,但无论那里提到的似乎我已经做到了。

这是我迄今为止尝试过的——

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'
}

清单 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>

样式 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>

看起来一切就绪,但我仍然面临这个奇怪的问题。

谁能帮我找出问题所在?提前致谢!

【问题讨论】:

    标签: java android android-theme android-textinputlayout


    【解决方案1】:

    不要使用您的应用上下文来创建样式视图,因为样式不适用于此上下文。

    new TextInputLayout(getApplicationContext());

    你应该使用你的活动上下文

    new TextInputLayout(this);
    

    【讨论】:

    • 哇!那太傻了。 :D 它就像一个魅力!不应低估愚蠢的变化。 :P 非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2013-05-24
    相关资源
    最近更新 更多