Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CardView onClickListener转到另一个目的地_Java_Android Studio 3.5 - Fatal编程技术网

Java CardView onClickListener转到另一个目的地

Java CardView onClickListener转到另一个目的地,java,android-studio-3.5,Java,Android Studio 3.5,我试图编写这样的代码,当点击任何cardview时,它会移动到另一个活动屏幕。下面是我的xml和java部分代码。应用程序运行时,可以显示cardview,但新活动不会打开 这是XML的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas

我试图编写这样的代码,当点击任何cardview时,它会移动到另一个活动屏幕。下面是我的xml和java部分代码。应用程序运行时,可以显示cardview,但新活动不会打开

这是XML的代码

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@drawable/homepage"
    android:gravity="center"
    android:id="@+id/LinearLayout1">

    <!-- A CardView that contains a TextView -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <LinearLayout
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.cardview.widget.CardView
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:id="@+id/mypet"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center">


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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"
                        android:layout_marginTop="10dp"
                        android:layout_gravity="center"
                        android:text="My Pet"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="10dp"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="Information about your Pet"
                        android:padding="5dp"
                        android:textColor="@android:color/darker_gray"/>

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/health"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"
                        android:layout_marginTop="10dp"
                        android:text="Track Health"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="10dp"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="Follow the health of your Pet"
                        android:padding="5dp"
                        android:textColor="@android:color/darker_gray"/>

                </LinearLayout>
            </androidx.cardview.widget.CardView>

        </LinearLayout>

        <LinearLayout
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.cardview.widget.CardView
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:id="@+id/vetVisit"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"
                        android:layout_marginTop="10dp"
                        android:layout_gravity="center"
                        android:text="Visit Vet"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="10dp"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="Details about the medical schedule"
                        android:padding="5dp"
                        android:textColor="@android:color/darker_gray"/>

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/overall"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"
                        android:layout_marginTop="10dp"
                        android:text="Track Health"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/lightgray"
                        android:layout_margin="10dp"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="Follow the health of your Pet"
                        android:padding="5dp"
                        android:textColor="@android:color/darker_gray"/>

                </LinearLayout>
            </androidx.cardview.widget.CardView>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private CardView petCard, healthCard,vetCard,overallCard;

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

        petCard = (CardView) findViewById(R.id.mypet);
        healthCard = (CardView) findViewById(R.id.health);
        vetCard = (CardView) findViewById(R.id.vetVisit);
        overallCard = (CardView) findViewById(R.id.overall);


    }


    @Override
    public void onClick(View v) {

        Intent i;

        switch (v.getId()){

            case R.id.mypet:

                i= new Intent(this,MyPetPage.class);
                startActivity(i);
                break;

            case R.id.health:

                i= new Intent(this,PetHealth.class);
                startActivity(i);
                break;

            case R.id.vetVisit:

                i= new Intent(this,VetPage.class);
                startActivity(i);
                break;

            case R.id.overall:

                i= new Intent(this,overall.class);
                startActivity(i);
                break;

                default:break;

        }

    }

}