Reputation: 129
Error:Caused by: java.lang.NullPointerException: Attempt to invoke
virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
tried with getSupportActionBar() also not able to resolve the exception please help me out in resolving this
I tried with v7 version of ActionBarActivity, extended the class with AppCompactActivity still not able to resolve.
Executed on both emulator and on real device same issue. After opening the app its its showing unfortunately "app" stopped working.
package pizzandburger.hfad.com.pizzaandburger;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
private String[] titles;
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ShareActionProvider shareActionProvider;
private int currentPosition=0;
private class DrawerItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles=getResources().getStringArray(R.array.items);
drawerList=(ListView)findViewById(R.id.drawer);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
drawerList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1,titles));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
if(savedInstanceState!=null){
currentPosition=savedInstanceState.getInt("position");
setActionBarTitle(currentPosition);
}
else{
selectItem(0);
}
drawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open_drawer,R.string.close_drawer){
@Override
public void onDrawerClosed(View view){
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView){
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
FragmentManager fragmentManager=getFragmentManager();
Fragment fragment=fragmentManager.findFragmentByTag("visible_fragment");
if(fragment instanceof TopFragment){
currentPosition=0;
}
else if(fragment instanceof PizzaFragment){
currentPosition=1;
}
else if(fragment instanceof BurgerFragment){
currentPosition=2;
}
else if(fragment instanceof OtherFragment){
currentPosition=3;
}
else if(fragment instanceof BeveragesFragment){
currentPosition=4;
}
setActionBarTitle(currentPosition);
drawerList.setItemChecked(currentPosition,true);
}
});
}
private void selectItem(int position){
Fragment fragment;
currentPosition=position;
switch (position){
case 1:
fragment=new PizzaFragment();
break;
case 2:
fragment=new BurgerFragment();
break;
case 3:
fragment=new OtherFragment();
break;
case 4:
fragment=new BeveragesFragment();
break;
default:
fragment= new TopFragment();
break;
}
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment, "visible_fragment");
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
setActionBarTitle(position);
drawerLayout.closeDrawer(drawerList);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("position",currentPosition);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
boolean drawerOpen=drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_share).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void setActionBarTitle(int position){
String title;
if(position==0){
title=getResources().getString(R.string.app_name);
}
else{
title=titles[position];
}
/*******************************/
/**** here its giving error *****/
/*******************************/
getActionBar().setTitle(title);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem=menu.findItem(R.id.action_share);
shareActionProvider=(ShareActionProvider)menuItem.getActionProvider();
setIntent("this is some simple text for sharing through shareActionProvider");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuitem){
if(drawerToggle.onOptionsItemSelected(menuitem)){
return true;
}
switch (menuitem.getItemId()){
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(menuitem);
}
}
private void setIntent(String text){
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,text);
shareActionProvider.setShareIntent(intent);
}
}
Here is my styles.xml
<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>
and here is my activity_main.xml
<?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/drawer_layout"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"></FrameLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="@+id/drawer"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffffff"></ListView>
</android.support.v4.widget.DrawerLayout>
Upvotes: 2
Views: 2035
Reputation: 81
If you are using an INTENT with AppCompatActivity, you need to access to the same relevant thread:
YourClass mContext;
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
mContext.getSupportActionBar().hide();
}
});
YourClass is the one extending AppCompActivity:
public class YourClass extends AppCompatActivity {
}
Upvotes: 0
Reputation: 129
Its the problem in the style sheet That says
"<style name="AppTheme.NoActionBar">"
Define some theme then you get rid of null pointer exception
Upvotes: 1
Reputation: 288
Some premisses:
import android.support.v7.app.AppCompatActivity
and your activity is extended it.requestWindowFeature(Window.FEATURE_ACTION_BAR);
or in your style:
<item name="android:windowActionBar">true</item>
regards,
Upvotes: 1