2017-01-04 5 views
2

Je crée une application Android. Lors de l'exécution de ma demande, je reçois l'exception d'exécution suivantes:Android Runtime Exception: java.lang.IllegalStateException: Vous devez utiliser un thème Theme.AppCompat (ou un descendant) avec cette activité

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ibm.myapp/com.ibm.myapp.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6077) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
      at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:347) 
      at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:316) 
      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:281) 
      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
      at com.ibm.myapp.MainActivity.onCreate(MainActivity.java:21) 
      at android.app.Activity.performCreate(Activity.java:6662) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
      at android.app.ActivityThread.-wrap12(ActivityThread.java)  
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
      at android.os.Handler.dispatchMessage(Handler.java:102)  
      at android.os.Looper.loop(Looper.java:154)  
      at android.app.ActivityThread.main(ActivityThread.java:6077)  
      at java.lang.reflect.Method.invoke(Native Method)  
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

J'ai essayé de résoudre ce problème en examinant des réponses à des questions similaires, telles que this question et this one, mais ces solutions ne semble pas pour résoudre le problème. Les deux fichiers styles.xml ont des styles qui sont des descendants du thème Theme.AppCompat.

styles.xml:

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <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" /> 

styles.xml (v21)

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
    <item name="android:statusBarColor">@android:color/transparent</item> 
</style> 

Et ce thème est répertorié dans le fichier AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.ibm.myapp"> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 


<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="@android:style/Theme.NoTitleBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

Fianlly, voici mon MainActivity.java:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 


public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_tasks) { 
      // start task activity 
      Intent intent = new Intent(this, TaskActivity.class); 
      startActivity(intent); 
     } else if (id == R.id.nav_goals) { 
      /* 
      Intent intent = new Intent(this, GoalActivity.class); 
      startActivity(intent); 
      */ 
     } else if (id == R.id.nav_schedule) { 
      /* 
      Intent intent = new Intent(this, ScheduleActivity.class); 
      startActivity(intent); 
      */ 
     } else if (id == R.id.nav_manage) { 
      //settings 
     } else if (id == R.id.nav_share) { 
     } else if (id == R.id.nav_send) {   
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

Apprécierions vraiment une aide pour résoudre ce problème car j'ai essayé tout ce que je peux penser, merci !

Répondre

1
<activity 
    android:name=".MainActivity" 
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar"> 

@android:style/Theme.NoTitleBar ne possèdes pas de Theme.AppCompat.

+0

J'ai complètement raté ça! Je l'ai changé pour: android: theme = "@ style/AppTheme" et il fonctionne maintenant, merci. – sums22