我试图将折叠的工具栏添加到Fragment,但是打开Fragment时应用程序崩溃了: 这是我得到的错误:
2018-10-19 18:05:38.950 13301-13301/io.github.davidwickerhf.diceroller E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.github.davidwickerhf.diceroller, PID: 13301
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at io.github.davidwickerhf.diceroller.ProfileFragment.onCreateView(ProfileFragment.java:36)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:809)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7436)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
片段Java:
public class ProfileFragment extends Fragment {
private MainActivity mMainActivity;
private SettingsViewModel settingsViewModel;
private SettingAdapter adapter;
private CoordinatorLayout coordinatorLayout;
private View fragmentView;
private TabLayout mTabLayout;
private ViewPager mViewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.fragment_profile, container, false);
mMainActivity = new MainActivity();
androidx.appcompat.widget.Toolbar mToolbar = fragmentView.findViewById(R.id.profile_toolbar);
mMainActivity.setSupportActionBar(mToolbar);
mViewPager = (ViewPager) fragmentView.findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(mMainActivity.getSupportFragmentManager());
adapter.AddFragment(new ProfileInfoFragment(), "Profile");
adapter.AddFragment(new ProfileMessagingFragment(), "Messages");
mViewPager.setAdapter(adapter);
mTabLayout = (TabLayout) fragmentView.findViewById(R.id.tab_layout_profile);
mTabLayout.setupWithViewPager(mViewPager);
return fragmentView;
}
}
片段XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ProfileFragment"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme"
app:toolbarId="@id/profile_toolbar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<ImageView
android:id="@+id/background_image_Toolbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:layout_marginTop="24dp"
android:id="@+id/profile_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
android:id="@+id/tab_layout_profile"
app:tabTextColor="@color/greyText"
app:tabIndicatorColor="@color/greyText"
android:background="#fafafa"
android:elevation="0dp">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.viewpager.widget.ViewPager>
请帮助?
答案 0 :(得分:0)
我猜您正在收到该错误,因为您正在实例化一个新的MainActivity
对象:
mMainActivity = new MainActivity();
对于Fragments
,您可以调用getActivity()
来获取托管Fragment
的活动的引用。像这样:
private MainActivity mMainActivity;
....
mMainActivity = (MainActivity) getActivity();
或者,由于您正在调用通用方法(可在MainActivity
上使用,因此您甚至无需强制转换为Activity
private Activity mMainActivity;
....
mMainActivity = getActivity();