My Android Advanced tour------> Fix error: You need to use a theme.appcompat Theme (or descendant) with this activity.

Source: Internet
Author: User

#1, error description

Today, to achieve activity does not show the title bar effect, in the project's Androidmanifest.xml file, the corresponding activity to add properties

 android:theme="@android:style/Theme.NoTitleBar"

The specific code is as follows:

<activity android:name=". Module.view.activity.KuwoMusicPlayActivity"  Android:theme="@android: Style/theme.notitlebar">                        <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter></activity>

As a result, when you run the app, the following error is reported:

touse a Theme.AppCompat theme (orwith this activity.

The specific error log is as follows:

 on- on  on: -:41.950 6637-6637/com. XTC. KuwoE/androidruntime:fatal Exception:main Process:com. XTC. KuwoPid:6637Java. Lang. RuntimeException: Unable to start activity componentinfo{com. XTC. Kuwo/com. XTC. Kuwo. Module. View. Activity. Kuwomusicplayactivity}: Java. Lang. IllegalStateException: Need to use a Theme. AppCompatTheme (ordescendant) with this activity. At Android. App. Activitythread. Performlaunchactivity(Activitythread. Java:2325) at Android. App. Activitythread. Handlelaunchactivity(Activitythread. Java:2387) at Android. App. Activitythread. Access$800 (Activitythread. Java:151) at Android. App. Activitythread$H. Handlemessage(Activitythread. Java:1303) at Android. OS. Handler. DispatchMessage(Handler. Java:102) at Android. OS. Looper. Loop(Looper. Java:135) at Android. App. Activitythread. Main(Activitythread. Java:5254) at Java. Lang. Reflect. Method. Invoke(Native Method) at Java. Lang. Reflect. Method. Invoke(Method. Java:372) atcom. Android. Internal. OS. Zygoteinit$MethodAndArgsCaller. Run(Zygoteinit. Java:955) atcom. Android. Internal. OS. Zygoteinit. Main(Zygoteinit. Java: -) caused By:java. Lang. IllegalStateException: Need to use a Theme. AppCompatTheme (ordescendant) with this activity. At Android. Support. V7. App. APPCOMPATDELEGATEIMPLV7. Createsubdecor(AppCompatDelegateImplV7. Java:340) at Android. Support. V7. App. APPCOMPATDELEGATEIMPLV7. Ensuresubdecor(AppCompatDelegateImplV7. Java:309) at Android. Support. V7. App. APPCOMPATDELEGATEIMPLV7. Setcontentview(AppCompatDelegateImplV7. Java:273) at Android. Support. V7. App. Appcompatactivity. Setcontentview(appcompatactivity. Java:136) atcom. XTC. Kuwo. Module. View. Activity. Kuwomusicplayactivity. OnCreate(kuwomusicplayactivity. Java: $) at Android. App. Activity. Performcreate(Activity. Java:5990) at Android. App. Instrumentation. Callactivityoncreate(instrumentation. Java:1106) at Android. App. Activitythread. Performlaunchactivity(Activitythread. Java:2278) at Android. App. Activitythread. Handlelaunchactivity(Activitythread. Java:2387)? At Android. App. Activitythread. Access$800 (Activitythread. Java:151)? At Android. App. Activitythread$H. Handlemessage(Activitythread. Java:1303)? At Android. OS. Handler. DispatchMessage(Handler. Java:102)? At Android. OS. Looper. Loop(Looper. Java:135)? At Android. App. Activitythread. Main(Activitythread. Java:5254)? At Java. Lang. Reflect. Method. Invoke(Native Method)? At Java. Lang. Reflect. Method. Invoke(Method. Java:372)? Atcom. Android. Internal. OS. Zygoteinit$MethodAndArgsCaller. Run(Zygoteinit. Java:955)? Atcom. Android. Internal. OS. Zygoteinit. Main(Zygoteinit. Java: -)?

As shown in the following:

2. Error analysis

It turns out that my Activity inherits from Android.support.v7.app.AppCompatActivity, not android.app.Activity.

3, Solution Method 1:

When the activity is created, if it is not so strongly required to inherit from Appcompatactivity, the activity is directly inherited.
If you inherit activity from Appcompatactivity:

publicclass MainActivity extends ActionBarActivity 

Change to activity to inherit from activity:

publicclass MainActivity extends Activity
Method 2:

Or want to inherit from Appcompatactivity, then follow the prompts to use the AppCompat theme, the Androidmanifest.xml file about the activity of the theme configuration changed to:

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

The complete code is as follows:

<activity android:name=". Module.view.activity.KuwoMusicPlayActivity"  Android:theme="@style/theme.appcompat.light.noactionbar">                        <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter></activity>

Problem Solving!

Ouyangpeng welcome reprint, sharing with people is the source of progress!
Reprint please keep the original address: Http://blog.csdn.net/ouyang_peng

My Android Advanced tour------> Fix error: You need to use a theme.appcompat Theme (or descendant) with this activity.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.