Reputation: 4460
I'm trying create an CustomActionBar with ActionBarDrawerToggle on Android v7. When I execute my project and try opened the Activity does throws an exception NullPointeException in this line: setDrawerListener(tg);
.
Here how I am trying.
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import br.com.williarts.batalhajuridica.R;
import br.com.williarts.batalhajuridica.frags.PerfilFrag;
public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
private ActionBar ab;
private DrawerLayout dl;
private ListView lv;
private ActionBarDrawerToggle tg;
private String[] fragments;
private CharSequence tl; //titulo principal
private CharSequence tlf; //titulo fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_drawerlayout);
init();
if(savedInstanceState == null){
selectedItem(0);
}
}
private void init(){
//actionbar
onConfigActionBar();
//listview
lv = (ListView)findViewById(R.id.lv);
fragments = getResources().getStringArray(R.array.itens_menu_string);
lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
lv.setOnItemClickListener(this);
//drawerlayout
dl = (DrawerLayout)findViewById(R.id.drawer_layout);
//actionbardrawertoggle
tg = new ActionBarDrawerToggle(this, dl, R.drawable.btmenu, R.string.nomeActionBar){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
dl.setDrawerListener(tg);
tl = tlf = getTitle();
}
/** ativa actionbar e botao home na action bar */
private void onConfigActionBar(){
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
tg.onConfigurationChanged(newConfig);
}
/** necessario */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
tg.syncState();
}
/** necessario */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (tg.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** necessario */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);
return true;
}
/** necessario */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean status = dl.isDrawerOpen(lv);
menu.findItem(R.id.action_settings).setVisible(!status);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
selectedItem(position);
}
/** seleciona o fragment q sera usado */
private void selectedItem(int position){
FragmentTransaction ft;
Fragment frag;
switch(position){
case 0:
frag = new PerfilFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.commit();
break;
}
lv.setItemChecked(position, true);
setCustomTitle(fragments[position]);
dl.closeDrawer(lv);
}
private void setCustomTitle(String title){
ab.setTitle(title);
tl = title;
}
}
Exception
11-27 18:57:50.540: E/AndroidRuntime(31167): FATAL EXCEPTION: main
11-27 18:57:50.540: E/AndroidRuntime(31167): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.williarts.batalhajuridica/br.com.williarts.batalhajuridica.draw.CustomDrawerLayout}: java.lang.NullPointerException
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.app.ActivityThread.access$700(ActivityThread.java:159)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.os.Looper.loop(Looper.java:176)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.app.ActivityThread.main(ActivityThread.java:5419)
11-27 18:57:50.540: E/AndroidRuntime(31167): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 18:57:50.540: E/AndroidRuntime(31167): at java.lang.reflect.Method.invoke(Method.java:525)
11-27 18:57:50.540: E/AndroidRuntime(31167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
11-27 18:57:50.540: E/AndroidRuntime(31167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
11-27 18:57:50.540: E/AndroidRuntime(31167): at dalvik.system.NativeStart.main(Native Method)
11-27 18:57:50.540: E/AndroidRuntime(31167): Caused by: java.lang.NullPointerException
11-27 18:57:50.540: E/AndroidRuntime(31167): at br.com.williarts.batalhajuridica.draw.CustomDrawerLayout.init(CustomDrawerLayout.java:65)
11-27 18:57:50.540: E/AndroidRuntime(31167): at br.com.williarts.batalhajuridica.draw.CustomDrawerLayout.onCreate(CustomDrawerLayout.java:36)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.app.Activity.performCreate(Activity.java:5372)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
11-27 18:57:50.540: E/AndroidRuntime(31167): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
11-27 18:57:50.540: E/AndroidRuntime(31167): ... 11 more
Upvotes: 0
Views: 846
Reputation: 8338
If your error is a NullPointerException, it can only be that one of the two things is null. Either dl
is null, or tg
is null. tg
doesn't appear to be null, so I'm convinced that it must be dl
.
Your issue is probably stemming from this line:
dl = (DrawerLayout)findViewById(R.id.drawer_layout);
Go into your XML and make sure that the layout has the id name "drawer_layout", then check that you're setting the right layout. Basically, that's probably your error.
Upvotes: 3