2016-03-29 11 views
1

私は動的にTextInputLayoutを追加しようとしています。 RadioButtonがクリックされるたびに、TextInputLayoutLinearLayoutの下に追加されます。TextInputLayoutを追加すると、動的に例外がスローされます

しかし、すべてこの後、私は例外取得しています -

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(); 
    } 
} 

アプリケーション(アプリ)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> 

すべてが所定の位置にあるが、私はまだこの奇妙な問題に直面していたように見えます。

何が問題になったのかを教えてもらえますか?前もって感謝します!

答えて

3

スタイリングがこのコンテキストでは機能しないため、アプリコンテキストを使用してスタイル付きビューを作成しないでください。

new TextInputLayout(getApplicationContext());

あなたはすごい

new TextInputLayout(this); 
+0

あなたの活動のコンテキストを使用する必要があります!それはとても愚かでした。 :Dそれは魅力のように働いた!愚かな変化を過小評価してはいけません。 :Pありがとう! :) –

関連する問題