安卓 java。lang.NullPointerException:尝试调用虚拟方法的ActionBar。空对象引用上的setNavigationMode(int)“”
我在运行时遇到这个错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.support.v7.app.ActionBar.setNavigationMode(int)' on a null object reference
主要活动。爪哇
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 = (安卓.support.v7.widget.RecyclerView) findViewById(R.id.my_recycler_view);
fab = (Button) findViewById(R.id.addButton);
drawerLayout = (安卓.support.v4.widget.DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setStatusBarBackground(R.color.primarydark);
toolbar = (安卓.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, 安卓.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(安卓.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.
}
}
主要活动。xml
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.v4.widget.DrawerLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:id="@+id/drawer_layout"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓: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
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:fitsSystemWindows="false">
<include layout="@layout/actionbarlayout"></include>
<include layout="@layout/content_view"></include>
<Button
安卓:id="@+id/addButton"
安卓:text="+"
安卓:textSize="25sp"
安卓:textColor="@安卓:color/white"
安卓:textAlignment="center"
安卓:layout_marginRight="15dp"
安卓:layout_marginBottom="15dp"
安卓:background="@drawable/circlebtn"
安卓:layout_width="56dp"
安卓:layout_height="56dp"
安卓:stateListAnimator="@animator/anim"
安卓:elevation="4dp"
style="?安卓:buttonStyleSmall"
安卓:layout_gravity="right|bottom" >
</Button>
</FrameLayout>
<com.androprogrammer.test.materialapp1.ScrimInsetsFrameLayout
安卓:id="@+id/container"
安卓:layout_width="wrap_content"
安卓:layout_height="match_parent"
安卓:fitsSystemWindows="true"
安卓:elevation="8dp"
安卓:layout_gravity="start"
app:insetForeground="#4000">
<ListView
安卓:id="@+id/list_drawer"
安卓:layout_width="@dimen/drawer_width"
安卓:layout_height="match_parent"
安卓:choiceMode="singleChoice"
安卓:background="@安卓:color/white"
安卓:fitsSystemWindows="true">
</ListView>
</com.androprogrammer.test.materialapp1.ScrimInsetsFrameLayout>
</安卓.support.v4.widget.DrawerLayout>
值/样式。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="安卓: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">@安卓:color/white</item>
</style>
</resources>
v21/样式。xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="安卓:windowDrawsSystemBarBackgrounds">true</item>
<item name="安卓:statusBarColor">@安卓:color/transparent</item>
<item name="安卓:windowContentTransitions">true</item>
<item name="安卓:windowAllowEnterTransitionOverlap">true</item>
<item name="安卓:windowAllowReturnTransitionOverlap">true</item>
<item name="安卓:windowSharedElementEnterTransition">@安卓:transition/move</item>
<item name="安卓:windowSharedElementExitTransition">@安卓:transition/move</item>
</style>
</resources>
AndroidManifest。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.androprogrammer.test.materialapp1" >
<application
安卓:allowBackup="true"
安卓:icon="@drawable/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme" >
<activity
安卓:name=".MainActivity"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请帮忙:)
# 1 楼答案
以你的风格。xml您必须启用actionbar。如果actionbar被禁用(设置为false),那么它将不会在运行时添加到活动中(即
getActionBar()
返回null
)# 2 楼答案
尝试更改父样式
如果
ActionBarActivity
已被弃用,则应使用AppCompatActivity