我在运行时得到这个nullPointerException:
来自mainActivity的引起:java.lang.NullPointerException:尝试调用虚方法'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' 在空对象引用上
代码:
// Find the right method, call on correct element
function launchIntoFullscreen(element) {
if(element.requestFullscreen) {
element.requestFullscreen();
} else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if(element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if(element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
// Launch fullscreen for browsers that support it!
launchIntoFullscreen(document.documentElement); // the whole page
launchIntoFullscreen(document.getElementById("videoElement")); // any individual element
有问题的代码行是:
package com.example.vasilis.spangreek;
import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import adapter.ExpandableListViewAdapter;
import model.NavDrawerItem;
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ExpandableListView mExpandableListView;
private ActionBarDrawerToggle mActionBarDrawerToggle;
//nav drawer Title
private CharSequence mDrawerTitle;
//used to store app titles
private CharSequence mTitles;
//slide menu items
private String[] navMenuItems;
private String[] navSubMenuItems;
private TypedArray navMenuIcons;
private List<NavDrawerItem> groupList;
private List<NavDrawerItem> childList;
private Map<NavDrawerItem, List<NavDrawerItem>> mapList;
private ExpandableListViewAdapter mAdapter;
ActionBar mActionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitles = mDrawerTitle = getTitle();
//nav drawer icons
navMenuIcons = getResources().obtainTypedArray(R.array.nav_icons);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
createGroupList();
CreateChildList();
// Recycle the typed array
navMenuIcons.recycle();
mExpandableListView = (ExpandableListView)findViewById(R.id.list_slideMenu);
mAdapter = new ExpandableListViewAdapter(this, mapList, groupList);
mExpandableListView.setAdapter(mAdapter);
mActionBar = getActionBar();
// enabling action bar app icon and behaving it as toggle button
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setHomeButtonEnabled(true);
//toggle
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View drawerView) {
mActionBar.setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
mActionBar.setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mActionBarDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer);
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
if(savedInstanceState == null) {
//displayView(0);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mExpandableListView);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void setTitle(CharSequence title) {
mTitles = title;
getActionBar().setTitle(mTitles);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mActionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mActionBarDrawerToggle .onConfigurationChanged(newConfig);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mActionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createGroupList() {
//load slide menu items
navMenuItems = getResources().getStringArray(R.array.nav_items);
groupList =new ArrayList<NavDrawerItem>();
for (int i = 0 ; i < navMenuItems.length ; i++ ) {
groupList.add(i , new NavDrawerItem(navMenuItems[i], navMenuIcons.getResourceId(i, -1)));
}
}
private void CreateChildList() {
mapList = new LinkedHashMap<NavDrawerItem, List<NavDrawerItem>>();
navSubMenuItems = getResources().getStringArray(R.array.nav_sub_items);
childList = new ArrayList<>();
for ( NavDrawerItem item : groupList) {
if(item.getTitle().equalsIgnoreCase("learning Spanish")) {
for (int i = 0 ; i < navSubMenuItems.length ; i ++) {
childList.add(i, new NavDrawerItem(navSubMenuItems[i]));
}
}
mapList.put(item,childList);
}
}
private void setGroupIndicatorToRight() {
/* Get the screen width */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
mExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
}
// Convert pixel to dip
public int getDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
我使用:style name =“AppTheme”parent =“Theme.AppCompat.Light.DarkActionBar”
我已经使用了getSupportActionBar(),但我没有计算任何解决方案
答案 0 :(得分:26)
问题的原因是使用支持主题为MainActivity extend Activity
的{{1}}。这是不兼容的事情。
你需要哪个min sdk?
在代码style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"
中,您不需要MainActivity extends Activity
。使用AppCompatTheme
“
如果您使用name="AppTheme" parent="android:Theme.Light
,则应将Theme.AppCompat.Light.DarkActionBar
从Activity
延长,并使用AppCompatActivity
。
而不是:
getSupportActionBar()
使用:
public class MainActivity extends Activity {
而不是:
public class MainActivity extends AppCompatActivity {
使用:
getActionBar().setTitle(mTitles);
答案 1 :(得分:3)
此问题可能是由您的主题引起的。再次检查,并确保其为Theme.AppCompat.Light.DarkActionBar
。
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">true</item>
...
</style>
如果您的活动延长AppCompatActivity
或ActionBarActivity
,请致电getSupportActionBar()
。
答案 2 :(得分:1)
将assert getActionBar () != null;
放在mActionBar = getActionBar();
答案 3 :(得分:0)
在我的情况下,我忘了初始化我的工具栏,因此,在使用getSupportActionBar之前,我必须这样做:
appbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(appbar);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_nav_menu);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
答案 4 :(得分:0)
如果您像我一样在2019年检查此答案,则问题出在您的Android清单上:
<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
在此处查看文档:
https://developer.android.com/training/appbar/setting-up.html
您还请记住上一个来自TetianaDev的答案:
代替:
public class MainActivity extends Activity {
使用:
public class MainActivity extends AppCompatActivity {
,而不是:
getActionBar().setTitle(mTitles);
使用:
getSupportActionBar().setTitle(mTitles);
这对我有用。
答案 5 :(得分:0)
您应该尝试这个。我认为它将起作用。
Toolbar toolbar = findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.rectagle_with_black_outer,
GravityCompat.START);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
toolbar, R.string.navigation_drawer_close,
R.string.navigation_drawer_close) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
答案 6 :(得分:0)
这显然不是OP的问题,但其他人可能会发现它有用:对我来说,解决方案是在操作栏的配置之前调用setContentView(R.layout.activity_main)。 >