Why can't my phone run the app? The Mystery of values

Source: Internet
Author: User

Welcome to follow my GitHub, attention to my csdn, wonderful constantly!

csdn:http://blog.csdn.net/caroline_wendy/article/details/68923156

An open source Android project on GitHub, download the configuration, complete the build, install it on the phone, but it won't work. The compiled version of the Project (compilesdkversion) is 25 (7.1), the lowest compatible version (Minsdkversion) is 19 (4.4), the system version of the phone is 21 (5.0), The minimum operating conditions for the application have been met. However, in the same system version (7.1) of the simulator, the application runs normally.

When I run the app on my phone, the error is as follows:

E/androidruntime:fatal Exception:mainjava.lang.RuntimeException:Unable to start activity componentinfo{com. SAULMM. Cui/com. SAULMM. Cui. Homeactivity}:java.lang.IllegalStateException:You need to use a Theme. AppCompatTheme (ordescendant) with this activity. caused By:java. Lang. IllegalStateException: Need to use a Theme. AppCompatTheme (ordescendant) with this activity. At Android. Support. V7. App. APPCOMPATDELEGATEIMPLV9. Createsubdecor(AppCompatDelegateImplV9. Java:359) at Android. Support. V7. App. APPCOMPATDELEGATEIMPLV9. Ensuresubdecor(AppCompatDelegateImplV9. Java:328) at Android. Support. V7. App. APPCOMPATDELEGATEIMPLV9. Setcontentview(AppCompatDelegateImplV9. Java:289) at Android. Support. V7. App. Appcompatactivity. Setcontentview(appcompatactivity. Java: $) at Android. DataBinding. Databindingutil. Setcontentview(Databindingutil. Java:276) at Android. DataBinding. Databindingutil. Setcontentview(Databindingutil. Java:261) atcom. SAULMM. Cui. Homeactivity. OnCreate(homeactivity. Java: the)
Positioning

The problem originates from DataBindingUtil#setContentView , DataBindingUtil binding layout layouts.

// HomeActivity.java@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    binding = DataBindingUtil.setContentView(this, R.layout.activity_home);    // ...}

Called AppCompatActivity#setContentView to Activity bind layout layouts.

// DataBindingUtil.javapublicstaticsetContentViewint layoutId,        DataBindingComponent bindingComponent) {    activity.setContentView(layoutId);    // ...}

Finally, the activity Agent implements AppCompatDelegateImplV9 setContentView the specific logic of the implementation of the class. ensureSubDecor Create a custom layout by using a method, and DecorView Activity resId ensureSubDecor Then call createSubDecor method creation DecorView .

//AppCompatDelegateImplV9.java   @Override  public  void  setcontentview  (int  resId) {Ensuresubdecor ();    //Create and initialize Decorview  ViewGroup contentparent = (viewgroup) Msubdecor.findviewbyid (Android.    R.id.content);    Contentparent.removeallviews ();    Layoutinflater.from (Mcontext). Inflate (ResId, contentparent); Moriginalwindowcallback.oncontentchanged ();} private  void  ensuresubdecor         () {if  (!msubdecorinstalled) {Msubdecor = Createsubdecor ();    //...  }}

The

Createsubdecor method, which sets the style of the root layout Decorview based on the style theme applied (Theme), and performs the initialization. When not containing the Appcompattheme_windowactionbar property, the theme is not set and an exception is thrown illegalstateexception .

//Appcompatdelegateimplv9.java  //sets the style of the root layout decorview according to the layout style style  private  ViewGroup createsubdecor  () {TypedArray a = Mcontext.obtainstyledattributes (    R.styleable.appcompattheme);    //no layout properties  if  (!a.hasvalue (R.styleable.appcompattheme_windowactionbar))        {a.recycle ();        //the problem!  throw  new  IllegalStateException ( "You need to use a theme.appcompat Theme (or descendant) W    ith this activity. ");} //... }  

Why does the API 25 emulator Start, my phone (API) can not be started? The reason is simple because the theme resources for open source projects are set incorrectly. The default theme AndroidManifest is theme set in the properties.

<application android:theme="@style/AppTheme">

Click the IDE's AppTheme jump to declaration and find only one place, which is values-v23 declared in.

Reason

For resource Properties , the system defaults to find and match attributes that are below the current API level , ensuring that the high-version properties are not executed in the lower version. Because the higher version adds more new interfaces, the lower version cannot be found, forcing the use may cause an exception or even crash, so access to the higher version of the property is forbidden.

Solve

With the understanding of the problem, the solution is very simple. In order to support all systems above the minimum API , the default is to values/themes.xml add AppTheme attributes.

<style name="AppTheme" parent="Base.AppTheme"/>

The problem is small, but can not be ignored, otherwise it will only be available in some mobile phones, in some mobile phone crashes, not touch the mind. In development, the preference is to values add attributes to the default folder, and if additional support is required, add them in the other high versions values-vXX . Do you get it?

That ' s all! Enjoy it!

Why can't my phone run the app? The Mystery of values

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.