繁体   English   中英

如何在片段的自定义actionBar中附加项目的onClickListener

[英]How to attach onClickListener of an item in custom actionBar in fragments

我有一个包含4个片段的活动。 我们知道片段中显示的actionBar是父活动。 现在,我获得了每个片段的自定义视图,并且工作正常。 问题是,当我尝试访问动作栏的自定义布局中的项目时,我的应用程序崩溃并出现空指针异常。 这很奇怪,因为正在显示操作栏,但我无法获取它的小部件。 我知道我们可以使用菜单在片段中填充actionBar,但是我的要求不允许我使用它。 所以我必须使用actionBar。 我在这里尝试了不同的答案,其中大多数告诉您如何在片段中设置标题。 这是我的代码。

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    @SuppressLint("InflateParams")
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.user_profile_page, null);

    context = getActivity();
    commonUserProfile = new CommonClass(getActivity().getApplicationContext());

    //noinspection ConstantConditions
    ((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(R.layout.test);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    android.support.v7.app.ActionBar mActionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
    assert mActionBar != null;
    mActionBar.setDisplayShowCustomEnabled(true);
    mActionBar.setDisplayShowTitleEnabled(false);
    mActionBar.setDisplayHomeAsUpEnabled(false);

    userProfileSettings = (ImageView)v.findViewById(R.id.follow);
    userProfileSettings.setOnClickListener(new View.OnClickListener() { //null pointer on this line
        @Override
        public void onClick(View v) {
            userSettingsPage();
        }
    });

return v;
}

下面是logcat:

12-18 07:01:14.734  31949-31949/com.example.rekky E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rekky, PID: 31949
java.lang.NullPointerException
        at com.example.rekky.Fragments.ProfileFragment.onCreateView(ProfileFragment.java:90)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

修改您的零件代码

((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(R.layout.test);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    android.support.v7.app.ActionBar mActionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
    assert mActionBar != null;
    mActionBar.setDisplayShowCustomEnabled(true);
    mActionBar.setDisplayShowTitleEnabled(false);
    mActionBar.setDisplayHomeAsUpEnabled(false);

    //Here you are getting null pointer exception because v is not the custom layout which 
    //layout you are setting in the actionbar. So, its unable to find the ImageView inside that.


    userProfileSettings = (ImageView)v.findViewById(R.id.follow);
    userProfileSettings.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {
            userSettingsPage();
        }
    });

到以下

View actionBarView = LayoutInflater.from(getActivity()).inflate(R.layout.test, null);

android.support.v7.app.ActionBar mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
assert mActionBar != null;
mActionBar.setCustomView(actionBarView);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setDisplayShowCustomEnabled(true);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayHomeAsUpEnabled(false);
userProfileSettings = (ImageView) actionBarView.findViewById(R.id.follow);
userProfileSettings.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
               userSettingsPage();
            }
});

请让我知道问题。

尝试替换为:

userProfileSettings = (ImageView)v.findViewById(R.id.follow);

有了这个:

userProfileSettings = (ImageView) mActionBar.getCustomView().findViewById(R.id.follow);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM