[英]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.