首页 > 解决方案 > 使用意图启动新活动时未显示布局

问题描述

我有一个带有 6 个硬编码卡片视图的片段类。我希望用户能够点击它们并打开另一个活动。每个都对不同的活动开放。目前,当用户单击卡片视图时,它会打开新活动,但不显示布局。例如,当用户单击科学卡片视图时,它只显示一个空白页面,但不显示带有按钮的布局。

这是我的片段页面代码:

public class show_class extends Fragment implements 
View.OnClickListener {
private CardView science_cv, math_cv, social_studies_cv, english_cv, 
tech_cv, other_cv;
private DatabaseReference myRef ;
//List<show_class> list;
// private RecyclerView recycle;
// Button view;
public show_class() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_show_class, container, false);

    //find cardview's by ids

    science_cv = (CardView) view.findViewById(R.id.science);
    math_cv = (CardView) view.findViewById(R.id.math);
    social_studies_cv = (CardView) view.findViewById(R.id.social_studies);
    english_cv = (CardView) view.findViewById(R.id.English);
    tech_cv = (CardView) view.findViewById(R.id.tech);
    other_cv= (CardView) view.findViewById(R.id.other);
    //set onclicklistener to cardviews
    science_cv.setOnClickListener(this);
    social_studies_cv.setOnClickListener(this);
    math_cv.setOnClickListener(this);
    english_cv.setOnClickListener(this);
    tech_cv.setOnClickListener(this);
    other_cv.setOnClickListener(this);

    return view;




}
public void onClick (View v){
    Intent i;
    switch (v.getId()) {
        case R.id.science:
            i = new Intent(getContext(), Science_classes.class);
        case R.id.math: i = new Intent(getContext(), Math_classes.class);startActivity(i);break;
        case R.id.social_studies: i = new Intent(getContext(), Social_studies_classes.class);startActivity(i);break;
        case R.id.English: i = new Intent(getContext(), English_classes.class);startActivity(i);break;
        case R.id.tech: i = new Intent(getContext(), Technology_classes.class);startActivity(i);break;
        case R.id.other: i = new Intent(getContext(), Other_classes.class);startActivity(i);break;
        default:break;
    }
}

}

这是我的片段页面的 xml:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#a4a4a4">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <android.support.v7.widget.CardView
                android:clickable="true"
                android:focusable="true"
                android:foreground="?android:selectableItemBackground"

                android:id="@+id/science"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:padding="0dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:gravity="center"
                        android:background="@color/colorSecondary">
                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:src="@drawable/ic_launcher_background"
                            android:padding="10dp"/>
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Science"
                        android:textColor="#000"
                        android:textSize="18dp" />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="2dp"/>


                </LinearLayout>
            </android.support.v7.widget.CardView>
            <android.support.v7.widget.CardView
                android:onClick="show2"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:id="@+id/math"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:padding="0dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:gravity="center"
                        android:background="@color/colorSecondary">

                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:padding="10dp"
                            android:src="@drawable/ic_launcher_background" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Math"
                        android:textColor="#000"
                        android:textSize="18dp" />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="2dp"/>

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

        </LinearLayout>
        <LinearLayout
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <android.support.v7.widget.CardView
                android:onClick="show3"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:id="@+id/English"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:padding="0dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:gravity="center"
                        android:background="@color/colorSecondary">

                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:padding="10dp"
                            android:src="@drawable/ic_launcher_background" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="English"
                        android:textColor="#000"
                        android:textSize="18dp" />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="2dp"/>
                    />
                </LinearLayout>
            </android.support.v7.widget.CardView>
            <android.support.v7.widget.CardView
                android:onClick="show4"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:id="@+id/social_studies"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:padding="0dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:gravity="center"
                        android:background="@color/colorSecondary">

                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:padding="10dp"
                            android:src="@drawable/ic_launcher_background" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Social Studies"
                        android:textColor="#000"
                        android:textSize="18dp" />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="2dp"/>


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

        </LinearLayout>
        <LinearLayout
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <android.support.v7.widget.CardView
                android:onClick="show5"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:id="@+id/tech"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:padding="0dp">
                <LinearLayout

                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
                    <LinearLayout
                        android:background="@color/colorSecondary"
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:gravity="center"
                       >
                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:src="@drawable/ic_launcher_background"
                            android:padding="10dp"/>
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Technology"
                        android:textColor="#000"
                        android:textSize="18dp" />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="2dp"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>
            <android.support.v7.widget.CardView
                android:onClick="show6"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:id="@+id/other"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:padding="0dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:gravity="center"
                        android:background="@color/colorSecondary">

                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:padding="10dp"
                            android:src="@drawable/ic_launcher_background" />
                    </LinearLayout>

                    <TextView

                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Other"
                        android:textColor="#000"
                        android:textSize="18dp" />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="2dp"/>



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

        </LinearLayout>
    </LinearLayout>
</ScrollView></FrameLayout>

这是我要上的科学课的代码(所有人都一样):

public class Science_classes extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_science_classes);
 }
}

这是xml,只是一个简单的按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Science_classes">

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

标签: androidandroid-intent

解决方案


在第一种情况下有一个缺失 startActivity(i);和声明, 这就是点击科学后你会以数学案例结束并开始数学活动的原因。将不得不检查这是否是空白屏幕的问题。你的数学活动布局是空的吗?break;case R.id.science:


推荐阅读