如何使用Coordinator Layout从Detail活动加载

时间:2017-01-30 09:54:37

标签: android android-layout

我的协调器布局xml有问题,它来自详细活动类。我不知道是否点击RecyclerView的项目,然后我的应用程序强制关闭..请告诉我我的班级和我的xml有什么问题。

此处是详细信息类的代码

public class DetailActivity extends AppCompatActivity {

    private CoordinatorLayout coordinatorLayout;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private Toolbar toolbar;

    TextView nameTxt,propTxt,descTxt;
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        Intent i=this.getIntent();
        String name=i.getExtras().getString("NAME_KEY");
        String propellant=i.getExtras().getString("PROPELLANT_KEY");
        String desc=i.getExtras().getString("DESCRIPTION_KEY");
        String imageurl=i.getExtras().getString("IMAGEURL_KEY");

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initToolbar();

        coordinatorLayout =(CoordinatorLayout) findViewById(R.id.coordinator);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.maincollapsing);
        collapsingToolbarLayout.setTitle("NAME_KEY");

        nameTxt= (TextView) findViewById(R.id.nameTxtDetail);
        propTxt= (TextView) findViewById(R.id.propellantTxtDetail);
        descTxt= (TextView) findViewById(R.id.descDetailTxt);
        img= (ImageView) findViewById(R.id.mainbackdrop);

        nameTxt.setText(name);
        propTxt.setText(propellant);
        descTxt.setText(Html.fromHtml(desc));
        PicassoClient.downloadImage(this,imageurl,img);
    }

    private void initToolbar(){
        toolbar = (Toolbar) findViewById(R.id.maintoolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sample_actions, menu);
        return true;
    }

}

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator"
    android:background="@android:color/background_light"
    tools:context="com.tutorials.hp.ditkeu.m_DetailActivity.DetailActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/mainappbar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/maincollapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="48dp"
            app:expandedTitleMarginStart="10dp"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginBottom="16dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleTextAppearance="@style/CollapsedAppBarTopic">

            <ImageView
                android:id="@+id/mainbackdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/placeholder"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/maintoolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp">

                <LinearLayout
                    style="@style/Widget.CardContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/nameTxtDetail"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:textStyle="bold"
                        android:layout_alignParentStart="true"
                        android:paddingLeft="5dp"
                        android:text="Judul Berita"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textColor="@color/input_register_bg" />

                    <TextView
                        android:id="@+id/propellantTxtDetail"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:padding="5dp"
                        android:text="Tanggal "
                        android:textAppearance="? android:attr/textAppearanceSmall"
                        android:textColor="@color/input_register_bg" />

                    <TextView
                        android:id="@+id/descDetailTxt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Isinya... "
                        android:textColor="@color/input_login"
                        android:paddingLeft="5dp"/>
                    </LinearLayout>

                </android.support.v7.widget.CardView>

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

你打电话的原因之一

setSupportActionBar(toolbar);

initToolbar();

实际上你在onCreate和initToolbar()中两次调用setSupportActionBar(工具栏)
执行此操作。从onCreate中删除setSupportActionBar(toolbar)和getSupportActionBar()。setDisplayHomeAsUpEnabled(true) / p>