Cannot create AlertDialog: AppCompat error

前端 未结 2 1624
长发绾君心
长发绾君心 2020-12-12 06:06

I am trying to create an AlertDialog like this:

counterButton.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLong         


        
相关标签:
2条回答
  • 2020-12-12 06:36

    Since your theme is related to your Activity, you must pass it as the context to AlertDialog.Builder - getApplicationContext() has no theme attached to it which is why you are getting an error.

    0 讨论(0)
  • 2020-12-12 06:44

    as error said

    You need to use a Theme.AppCompat theme
    

    then you will create style extend AppTheme

    !-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base"/>
    
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    
    
    
    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
    0 讨论(0)
提交回复
热议问题