【发布时间】:2016-09-04 18:53:29
【问题描述】:
我在使用这个时遇到空指针错误。
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
在我的扩展 ListActivity 的 java 类中 将我的主题升级到 Theme.AppCompat 后,我收到此错误。 我检查了stackoverflow上给出的所有解决方案,但没有任何帮助。
主活动
import android.support.v7.app.ActionBar;
......
public class MainActivity extends AppCompatActivity
{
.....
@Override
protected void onCreate(Bundle savedInstanceState)
{
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the two
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
//if a tab was changed by a swipe gesture
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position); //update the tab bar to match the selected page
mDisplayedFragIndex=position; //update the index of the currently displayed frag
if (position==1) //if the view has moved to the history fragment:
{
mHistoryFrag.loadHistory(); //reload the history list view
}
invalidateOptionsMenu();
}
});
ActionBar.TabListener tabListener=new ActionBar.TabListener(){
@Override
public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
};
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++)
{
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(tabListener));
}//for
.....
}
这里是xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>
我在这里使用 viewpager 我应该怎么做我在 activity_main.xml 中声明工具栏 xml 和在主要活动中的代码时仍然出现错误。
Activity2 //在这个活动中我遇到了错误。 ....
public class Activity2 extends ListActivity
{
.....
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
setupActionBar();
}
private void setupActionBar()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
// enables the activity icon as a 'home' button. required if "android:targetSdkVersion" > 14
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
Activity2 扩展 ListFragment
public class Activity2 extends ListFragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_chat,
container, false);
setupActionBar();
return view;
}
private void setupActionBar()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
// enables the activity icon as a 'home' button. required if "android:targetSdkVersion" > 14
getSupportActionBar().setHomeButtonEnabled(true); **//Getting Error in this line "Cannot Resolved "**
//getActionBar().setHomeButtonEnabled(true);
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}//end of class
【问题讨论】:
-
发布完整的 code 与 extends class。
-
更新了我的问题
-
将
Activity2类更改为extends ActionBarActivity。 -
@jaydroider firstofall ActionBarActivity 已被弃用,第二件事我在扩展此功能后无法使用“getListView()”
标签: java android material-design