ABS与SherlockFragmentActivity

时间:2014-02-21 20:51:42

标签: java android actionbarsherlock android-fragmentactivity

我正在使用Fragments实现ActionBarSherlock。我已经做了很多次但从未使用Fragments。我不确定是否必须在每个片段中实现它,或者只是在FragmentActivity中使用它就足够了。每当我点击ActionBar的任何项目时,我都会得到运行时NullPointerException。

这是我的FragmentActivity。 附:我所有的片段都在扩展SherlockFragment。

package com.vipulk.acuv;


public class ViewPagerFragmentActivity extends SherlockFragmentActivity{
/** maintains the pager adapter*/
private PagerAdapter mPagerAdapter;

com.actionbarsherlock.app.ActionBar actionbar;
com.actionbarsherlock.view.Menu menu;


Context context;
/* (non-Javadoc)
 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.layout);
    //initialsie the pager
    this.initialisePaging();

    initializeAbs();

}




private void initializeAbs() {
    // TODO Auto-generated method stub

    MyApplication myapp=(MyApplication)getApplicationContext();


    actionbar=getSupportActionBar();
    getSupportActionBar().setIcon(R.drawable.ic_add_user);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);


}

/**
 * Initialise the fragments to be paged
 */
private void initialisePaging() {

    //Set the pager with an adapter




    List<SherlockFragment> fragments = new Vector<SherlockFragment>();
    fragments.add((SherlockFragment) SherlockFragment.instantiate(this, Tab1Fragment.class.getName()));
    fragments.add((SherlockFragment) SherlockFragment.instantiate(this, ChangedContact.class.getName()));
    fragments.add((SherlockFragment) SherlockFragment.instantiate(this, Friends.class.getName()));
    this.mPagerAdapter  = new com.vipulk.acuv.PagerAdapter(getSupportFragmentManager(), fragments);
    //
    ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);
    pager.setCurrentItem(1, false);
    pager.setOffscreenPageLimit(3);

}



public boolean onPrepareOptionsMenu(com.actionbarsherlock.view.Menu menu) {
     com.actionbarsherlock.view.MenuInflater inflater= getSupportMenuInflater();

        inflater.inflate(R.menu.menu_register, menu);   

        return super.onPrepareOptionsMenu(menu);
    }



    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {




        switch (item.getItemId()) {

        case R.id.register:

                Intent i=new Intent(ViewPagerFragmentActivity.this, Registration.class);
                startActivity(i);

                return true;




            default:
                return super.onOptionsItemSelected(item);
        }

    }

}

这是日志:

02-21 20:44:45.428: W/dalvikvm(2556): threadid=1: thread exiting with uncaught exception           (group=0x40a71930)
02-21 20:44:45.498: E/AndroidRuntime(2556): FATAL EXCEPTION: main
02-21 20:44:45.498: E/AndroidRuntime(2556): java.lang.NullPointerException
02-21 20:44:45.498: E/AndroidRuntime(2556):     at         com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:496)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at     java.lang.StringBuilder.append(StringBuilder.java:202)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:201)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at android.view.View.performClick(View.java:4204)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at android.view.View$PerformClick.run(View.java:17355)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at android.os.Handler.handleCallback(Handler.java:725)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at android.os.Looper.loop(Looper.java:137)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at java.lang.reflect.Method.invoke(Method.java:511)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 20:44:45.498: E/AndroidRuntime(2556):     at dalvik.system.NativeStart.main(Native Method)
02-21 20:44:49.238: I/Process(2556): Sending signal. PID: 2556 SIG: 9

2 个答案:

答案 0 :(得分:0)

这是ActionBarSherlock中的一个错误。固定它。 ABS的Activity com.android.internal.view.menu.MenuItemImpl有一个方法toString(),它给出了NullPointerException,因为我的菜单项没有设置“androi:title”属性。如果您遇到类似问题,只需在菜单项中添加“android:title”属性(在xml菜单中)

答案 1 :(得分:0)

这是一个错误。在菜单项中使用Android:title属性。