2015-12-30 37 views
0

我的MainActivity类别:您需要使用Theme.AppCompat主题上AlertDialog创作

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 
       .... 
       ... 
} 

Style.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"> 
     <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> 

当我运行该应用程序,然后它工作完美,但是当我我想从我的适配器类创建/显示AlertDialog.Builder然后提示错误

`You need to use a Theme.AppCompat theme (or descendant) with this activity.at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:124)` 

清单文件:

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     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> 

AlertDialog代码:

public class mainviewAdapter extends ArrayAdapter<MainCategory> 
{ 

AlertDialog.Builder builderSingle; 
public mainviewAdapter (Context context, ArrayList<MainCategory> values) { 
     super(context, R.layout.home_rt, values); 

     this.context = context; 
     this.values = values; 
     builderSingle = new AlertDialog.Builder(context); 
     builderSingle.setIcon(R.mipmap.ic_launcher); 
     builderSingle.setTitle("Select Your Category"); 


    } 

    on button click: 

    builderSingle.setNegativeButton("cancel", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 

     builderSingle.setAdapter(arrayAdapter, 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
        } 
       }); 

     builderSingle.show(); 

我怎样才能解决上Alertdialog这个错误?与

builderSingle = new AlertDialog.Builder(context); 

builderSingle = new AlertDialog.Builder(context,R.style.dialogTheme); 

添加代码在你style.xml :

<style name="dialogTheme" parent="@style/Theme.AppCompat"> 
<!-- Any customizations for your app running on devices with Theme.Holo here --> 
</style> 

+0

共享代码知道问题 –

+0

@kapilrajput增加了对alertdialog – deepak

回答

0

尝试更换您的代码行设置主题,以您的AlertDialog并记住一件事要避免Unable to add window -- token null is not for an application异常而不是getApplicationContext(),只需使用AlertDialog.Builder的Activityname.this

+0

代码试试这个代码我得到'无法添加窗口后 - 令牌null不是在Android的应用程序 .view.ViewRootImpl.setView(ViewRootImpl.java:641)'错误。 – deepak

+0

我已经编辑我的答案现在尝试它将工作并使用Activityname.this作为上下文,同时创建您的类的对象MainviewAdapter –

+0

仍然无法正常工作。 – deepak

相关问题