Floo-Lapi Posté(e) 11 février 2016 Share Posté(e) 11 février 2016 Bonjour je débute en développement Android et je suis actuellement un tuto pour créer un Navigation Drawer sur YT (Voici le lien : http://www.youtube.com/watch?v=Zuo3n-DjF7w ). Mais je suis confronté actuellement à une erreur dont je n'arrive pas à trouvé la solution.Normalement cela devrait rendre comme au stade du tuto où je suis (plus ou moins 17:40 min).Merci d'avance ;) Voici les erreurs : 02-10 11:31:12.432 2297-2297/com.example.flori.mylordring E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.flori.mylordring, PID: 2297 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flori.mylordring/com.example.flori.mylordring.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) 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) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference at com.example.flori.mylordring.MainActivity.onCreate(MainActivity.java:31) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) 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) voici le code du Main : package com.example.flori.mylordring; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static ImageButton imgView; private int test; int [] images = {R.drawable.im1,R.drawable.im2,R.drawable.im3,R.drawable.im4,R.drawable.im5,R.drawable.im6,R.drawable.im7}; @[member=override] protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @[member=override] public void onClick(View view) { Snackbar.make(view, "Rolololo", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } public void image(View view){ imgView=(ImageButton)findViewById(R.id.imageButton); imgView.setOnClickListener(new View.OnClickListener() { @[member=override] public void onClick(View v){ test++; test = test % images.length; imgView.setImageResource(images[test]); } } ); } @[member=override] public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @[member=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); } } Voici le code de l'activité : <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawerLayout" android:background="#e2656262" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.flori.mylordring.MainActivity" tools:showIn="@layout/activity_main"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragmentholder"> </FrameLayout> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/navlist" android:background="#dedede" android:layout_gravity="start" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <ImageButton android:onClick="image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/im1" android:background="@android:color/transparent" android:layout_above="@+id/textView" android:layout_centerHorizontal="true" /> </android.support.v4.widget.DrawerLayout> Voici mon Manifest : <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.flori.mylordring"> <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="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> Citer Lien vers le commentaire Partager sur d’autres sites More sharing options...
andry57 Posté(e) 11 février 2016 Share Posté(e) 11 février 2016 A mon avis ce code : ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); doit être mis après setSupportActionBar(toolbar); Citer Lien vers le commentaire Partager sur d’autres sites More sharing options...
Recommended Posts
Rejoignez la conversation
Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.