java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ActionBar.setNavigationMode(int)”
Posted
技术标签:
【中文标题】java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ActionBar.setNavigationMode(int)”【英文标题】:java.lang.NullPointerException: Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference 【发布时间】:2015-08-17 23:02:41 【问题描述】:我在运行时收到此错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setNavigationMode(int)' on a null object reference
主Activity.java
public class MainActivity <T extends Fragment> extends ActionBarActivity implements ListView.OnItemClickListener
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tab;
tab = actionBar.newTab()
.setText("Categories")
.setTabListener(new TabListener<CategoriesFragment>(
this, "Categories", CategoriesFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Acceuil")
.setTabListener(new TabListener<AcceuilFragment>(
this, "Acceuil", AcceuilFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Topics")
.setTabListener(new TabListener<TopicsFragment>(
this, "Topics", TopicsFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Tendances")
.setTabListener(new TabListener<TendancesFragment>(
this, "Tendances", TendancesFragment.class));
actionBar.addTab(tab);
setRef();
//Set the custom toolbar
if (toolbar != null)
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.open,
R.string.close
)
public void onDrawerClosed(View view)
Log.d("MainActivity", "OnDrawerClosed");
super.onDrawerClosed(view);
invalidateOptionsMenu();
public void onDrawerOpened(View drawerView)
Log.d("MainActivity", "OnDrawerOpened");
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
;
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
@Override
protected void onPostCreate(Bundle savedInstanceState)
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
private void setRef()
// create reference to the xml views
mRecyclerView = (android.support.v7.widget.RecyclerView) findViewById(R.id.my_recycler_view);
fab = (Button) findViewById(R.id.addButton);
drawerLayout = (android.support.v4.widget.DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setStatusBarBackground(R.color.primarydark);
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
leftDrawerList = (ListView) findViewById(R.id.list_drawer);
View list_header = getLayoutInflater().inflate(R.layout.drawerlist_header, null);
leftDrawerList.addHeaderView(list_header);
navigationDrawerAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
leftDrawerList.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
Toast.makeText(getApplicationContext(), "Clicked on " + leftSliderData[position - 1], Toast.LENGTH_LONG).show();
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
// Check if the fragment is already initialized
if (mFragment == null)
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
else
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
if (mFragment != null)
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
// User selected the already selected tab. Usually do nothing.
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:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_
android:layout_
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- Setting this one to true makes the status bar
to have a non-transparent shitty background -->
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:layout_
android:layout_
android:fitsSystemWindows="false">
<include layout="@layout/actionbarlayout"></include>
<include layout="@layout/content_view"></include>
<Button
android:id="@+id/addButton"
android:text="+"
android:textSize="25sp"
android:textColor="@android:color/white"
android:textAlignment="center"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:background="@drawable/circlebtn"
android:layout_
android:layout_
android:stateListAnimator="@animator/anim"
android:elevation="4dp"
style="?android:buttonStyleSmall"
android:layout_gravity="right|bottom" >
</Button>
</FrameLayout>
<com.androprogrammer.test.materialapp1.ScrimInsetsFrameLayout
android:id="@+id/container"
android:layout_
android:layout_
android:fitsSystemWindows="true"
android:elevation="8dp"
android:layout_gravity="start"
app:insetForeground="#4000">
<ListView
android:id="@+id/list_drawer"
android:layout_
android:layout_
android:choiceMode="singleChoice"
android:background="@android:color/white"
android:fitsSystemWindows="true">
</ListView>
</com.androprogrammer.test.materialapp1.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primarydark</item>
<item name="colorAccent">@color/primary</item>
<item name="colorControlHighlight">@color/primary</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
</resources>
v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androprogrammer.test.materialapp1" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请帮忙:)
【问题讨论】:
你确定你不需要做任何其他事情来让它工作吗? ActionBar actionBar = getSupportActionBar();该方法似乎没有返回您期望的对象。 我知道 actionbar 返回 Null 我认为错误来自 Styles.xml 但我无法更正 你确定这个<T extends Fragment>
?
是的,我很确定
您在哪个版本的 android 中看到此错误? 3.*?
【参考方案1】:
尝试改变父样式
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
而ActionBarActivity
已弃用,您应该使用AppCompatActivity
【讨论】:
【参考方案2】:在您的 style.xml 中,您必须启用操作栏。如果 actionbar 被禁用(设置为 false),那么它不会在运行时添加到活动中(即getActionBar()
返回null
)。
【讨论】:
当我将它设置为 true 运行时显示另一个错误 此 Activity 已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。以上是关于java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ActionBar.setNavigationMode(int)”的主要内容,如果未能解决你的问题,请参考以下文章
亲測,Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerExce