如何在单个活动

时间:2016-03-02 13:26:45

标签: android-actionbar android-toolbar

这是我的MainActivity

公共类ProjectDetailsActivity扩展了AppCompatActivity {

private static final String TAG = ProjectDetailsActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_container);

    if (savedInstanceState == null) {
        addFragment(new ProjectDetailsFragment(), false);
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_detail);
    toolbar.setNavigationIcon(R.drawable.ic_action_back);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Button Clicked", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });



}

public void addFragment(Fragment f, boolean isAddToBack) {

    FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
    mFragmentTransaction.replace(R.id.fragment_container, f);
    if (isAddToBack) {
        mFragmentTransaction.addToBackStack(null);
    }
    mFragmentTransaction.commit();
}

@Override
public void onBackPressed() {

    super.onBackPressed();
    overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==android.R.id.home){
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}
  

这是我的xml文件   

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/toolbar_scrollable_image_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/title_bg" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:text="Garden Residency II"
            android:textColor="#ffffffff" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_detail"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<include
    android:id="@+id/include_layout"
    layout="@layout/project_detail_content" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end" />

  

这是我的片段类`

公共类ProjectDetailsFragment扩展BaseFragment实现View.OnClickListener {

private static final String TAG = ProjectDetailsFragment.class.getSimpleName();

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View mView = inflater.inflate(R.layout.fragment_details, container, false);

    mappingWidgets(mView);

    return mView;
}

private void mappingWidgets(View v) {


}

@Override
public void onClick(View v) {


}

}

错误日志: 致命异议:主要                                                                             java.lang.RuntimeException:无法启动活动ComponentInfo {}:java.lang.NullPointerException:尝试在android上的空对象引用上调用虚方法'void android.support.v7.widget.Toolbar.setNavigationIcon(int)'。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)                                                                                   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                   在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                                                                                引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.v7.widget.Toolbar.setNavigationIcon(int)'                                                                                   在com.abc.xyz.abcActivity.onCreate(ProjectDetailsActivity.java:32)                                                                                   在android.app.Activity.performCreate(Activity.java:6237)                                                                                   在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)                                                                                   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)                                                                                   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                   在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

  

Mainfest中的活动主题是 - @ style / Apptheme.Noactionbar

1 个答案:

答案 0 :(得分:2)

只需将这些代码放入片段中,我就可以从片段

访问操作栏
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View mView = inflater.inflate(R.layout.activity_detail, container, false);

    final Toolbar toolbar = (Toolbar) mView.findViewById(R.id.toolbar);


    AppCompatActivity activity = (AppCompatActivity) getActivity();
    activity.setSupportActionBar(toolbar);
    activity.setTitle("Project Details");
    activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    CollapsingToolbarLayout collapsingToolbar =
            (CollapsingToolbarLayout)mView.findViewById(R.id.collapsing_toolbar);