@Gfd

В чем может быть проблема с темой в android?

Написал вот такой код:
mTextViewIdea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText editText = new EditText(getApplicationContext());

                if (!mTextViewIdea.getText().toString().equals("None")) {
                    editText.setText(mTextViewIdea.getText());
                }
                new AlertDialog.Builder(getApplicationContext()).setTitle("Enter the text")
                        .setView(editText)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (mTextViewIdea.getText().toString().equals("None")) {
                                    if (!editText.getText().toString().equals("")) {
                                        mPerson.addNewIdea(editText.getText().toString(), getDate());
                                    } else {
                                        Toast.makeText(getApplicationContext(), "You have not entered text!", Toast.LENGTH_SHORT).show();
                                    }
                                } else {
                                    if (!editText.getText().toString().equals("")) {
                                        Idea idea = new Idea(mTextViewIdea.getText().toString(), getDate());
                                        mPerson.changeLastIdea(idea);
                                    } else {
                                        Toast.makeText(getApplicationContext(), "You have not entered text!", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        }).create().show();
            }
        });

Выдает ошибку:
08-08 11:58:34.947 4251-4251/com.example.root.lifeorganization E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.root.lifeorganization, PID: 4251
                                                                                 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                     at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
                                                                                     at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
                                                                                     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
                                                                                     at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:80)
                                                                                     at android.support.v7.app.AlertController.installContent(AlertController.java:214)
                                                                                     at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
                                                                                     at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
                                                                                     at android.app.Dialog.show(Dialog.java:295)
                                                                                     at com.example.root.lifeorganization.MainActivity$1$override.onClick(MainActivity.java:118)
                                                                                     at com.example.root.lifeorganization.MainActivity$1$override.access$dispatch(MainActivity.java)
                                                                                     at com.example.root.lifeorganization.MainActivity$1.onClick(MainActivity.java:0)
                                                                                     at android.view.View.performClick(View.java:5198)
                                                                                     at android.view.View$PerformClick.run(View.java:21147)
                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Вот файл styles.xml:
<resources>

    <style name="MyTheme" parent="MyTheme.Base">

    </style>
    <!-- Base theme applied no matter what API -->
    <style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">@color/primary</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">@color/primaryDark</item>
        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="colorAccent">@color/accent</item>
        <!-- You can also set colorControlNormal, colorControlActivated
            colorControlHighlight and colorSwitchThumbNormal. -->
    </style>

</resources>


Манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.root.lifeorganization">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/MyTheme">
        <activity android:name=".WelcomeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

</manifest>

PS: я не менял тему приложения (только цвет 'colorAccent' поменял на черный). Не понимаю, в чем может быть ошибка?
  • Вопрос задан
  • 349 просмотров
Решения вопроса 1
a13xsus
@a13xsus
Lazy developer
colorAccent, colorPrimary, colorPrimaryDark -- это опции AppCompat темы для андроид 5 и выше, либо для младших версий с использованием support library v7.

Подключаете суппорт либы, в values\styles.xml пишете что-то типа:

<style name="MyTheme" parent="MyTheme.Base">

 </style>
  <!-- Base theme applied no matter what API -->
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/primary</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>
     <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
  </style>


а в values\colors.xml уже определяете цвета:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>	
	<color name="primary">#607D8B</color>
	<color name="primaryDark">#455A64</color>
	<color name="accent">#FE4564</color>   
</resources>


И разумеется наследуем активити от AppCompatActivity

public class MainActivity extends AppCompatActivity {
  // ...
}


И в манифесте указываем использовать тему. В моем примере это MyTheme.

<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:theme="@style/MyTheme"></application>
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы