使用getActionBar.setTitle()时,应用程序崩溃;同时扩展FragmentActivity
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用getActionBar.setTitle()时,应用程序崩溃;同时扩展FragmentActivity相关的知识,希望对你有一定的参考价值。
应用程序崩溃在getActionBar.setTitle(....)中获取空指针异常?怎么解决?我试过getSupportActionBar而不是getActionBar ..但它不起作用?有人能帮我吗 ..
package com.msinfolab.mandy.tfq;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity{
private Fragment contentFragment;
QuotecardListFragment pdtListFragment;
FavoriteListFragment favListFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
/*
* This is called when orientation is changed.
*/
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("content")) {
String content = savedInstanceState.getString("content");
if (content.equals(FavoriteListFragment.ARG_ITEM_ID)) {
if (fragmentManager.findFragmentByTag(FavoriteListFragment.ARG_ITEM_ID) != null) {
setFragmentTitle(R.string.favorites);
contentFragment = fragmentManager
.findFragmentByTag(FavoriteListFragment.ARG_ITEM_ID);
}
}
}
if (fragmentManager.findFragmentByTag(QuotecardListFragment.ARG_ITEM_ID) != null) {
pdtListFragment = (QuotecardListFragment) fragmentManager
.findFragmentByTag(QuotecardListFragment.ARG_ITEM_ID);
contentFragment = pdtListFragment;
}
} else {
pdtListFragment = new QuotecardListFragment();
setFragmentTitle(R.string.app_name);
switchContent(pdtListFragment, QuotecardListFragment.ARG_ITEM_ID);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
if (contentFragment instanceof FavoriteListFragment) {
outState.putString("content", FavoriteListFragment.ARG_ITEM_ID);
} else {
outState.putString("content", QuotecardListFragment.ARG_ITEM_ID);
}
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_favorites:
setFragmentTitle(R.string.favorites);
favListFragment = new FavoriteListFragment();
switchContent(favListFragment, FavoriteListFragment.ARG_ITEM_ID);
return true;
}
return super.onOptionsItemSelected(item);
}
public void switchContent(Fragment fragment, String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
while (fragmentManager.popBackStackImmediate());
if (fragment != null) {
FragmentTransaction transaction = fragmentManager
.beginTransaction();
transaction.replace(R.id.content_frame, fragment, tag);
if (!(fragment instanceof QuotecardListFragment)) {
transaction.addToBackStack(tag);
}
transaction.commit();
contentFragment = fragment;
}
}
protected void setFragmentTitle(int resourseId) {
setTitle(resourseId);
getActionBar().setTitle(resourseId);
}
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
super.onBackPressed();
} else if (contentFragment instanceof QuotecardListFragment
|| fm.getBackStackEntryCount() == 0) {
finish();
}
}
}
这部分代码警告空指针异常..如何纠正它?
protected void setFragmentTitle(int resourseId) {
setTitle(resourseId);
getActionBar().setTitle(resourseId);
}
和另一个警告.. equals可能会产生空指针异常..如何解决这个问题
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("content")) {
String content = savedInstanceState.getString("content");
if (content.equals(FavoriteListFragment.ARG_ITEM_ID)) {
if (fragmentManager.findFragmentByTag(FavoriteListFragment.ARG_ITEM_ID) != null) {
setFragmentTitle(R.string.favorites);
contentFragment = fragmentManager
.findFragmentByTag(FavoriteListFragment.ARG_ITEM_ID);
}
}
}
if (fragmentManager.findFragmentByTag(QuotecardListFragment.ARG_ITEM_ID) != null) {
pdtListFragment = (QuotecardListFragment) fragmentManager
.findFragmentByTag(QuotecardListFragment.ARG_ITEM_ID);
contentFragment = pdtListFragment;
}
} else {
pdtListFragment = new QuotecardListFragment();
setFragmentTitle(R.string.app_name);
switchContent(pdtListFragment, QuotecardListFragment.ARG_ITEM_ID);
}
答案
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Profile");
希望它的工作
另一答案
创建工具栏布局,它应该是所有片段的常用工具栏。并为toolbar.setTitle(getString(R.string.menu_category));
等工具栏设置标题
另一答案
首先,你需要在片段布局XML中定义自己的工具栏,你做完了吗?
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
然后在你的onCreate方法中,在设置标题之前你需要这样做:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
最后在你的setFragmentTitle方法中,你设置工具栏标题如下:
((Toolbar) findViewById(R.id.toolbar)).setTitle(resourseId);
如果问题得到解决,请告诉我。
以上是关于使用getActionBar.setTitle()时,应用程序崩溃;同时扩展FragmentActivity的主要内容,如果未能解决你的问题,请参考以下文章
Android Activity 标题与 ActionBar 标题