使用意图启动新活动时布局未显示

时间:2019-06-05 13:05:42

标签: android android-intent

我有一个带有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" />

2 个答案:

答案 0 :(得分:1)

在第一种情况startActivity(i);中缺少break;case R.id.science:语句,这是在单击“科学”后您将获得数学案例并开始数学活动的原因。将不得不检查这是否是黑屏的问题。您的数学活动布局是否为空?

答案 1 :(得分:0)

'在onClick()的show_class片段中,您没有像在启动Science_classes活动。 “在R.id.science的第一种情况下”

startActivity(i);