java - 为什么当我单击按钮时我的页面不存在?

标签 java android xml

我的问题:

  1. 我的应用没有错误。
  2. 当我点击按钮时,我的页面不会显示。
  3. 当我点击某个地方而不是按钮时,它可以存在。

我已经通过youtube引用过,但我自己无法解决这个问题。

这是我的 java 文件: dashboard.java

package com.example.whatisthat;

import androidx.cardview.widget.CardView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class Dashboard extends Activity implements     View.OnClickListener
{

private CardView LoginBtn, CaptureBtn, AboutBtn, FeedbackBtn, IgBtn,     HelpBtn;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    //DEFINE CARDS
    //ADD CLICK LISTENER TO THE CARDS
    LoginBtn = findViewById(R.id.Login);
    LoginBtn.setOnClickListener(this);

    CaptureBtn = findViewById(R.id.Capture);
    CaptureBtn.setOnClickListener(this);

    AboutBtn = findViewById(R.id.About);
    AboutBtn.setOnClickListener(this);

    FeedbackBtn = findViewById(R.id.Feedback);
    FeedbackBtn.setOnClickListener(this);

    IgBtn = findViewById(R.id.Ig);
    IgBtn.setOnClickListener(this);

    HelpBtn = findViewById(R.id.Help);
    HelpBtn.setOnClickListener(this);
}

@Override
public void onClick(View view)
{
    Intent i;

    switch (view.getId())
    {
        case R.id.Login : i = new Intent(this,Login1.class);
        startActivity(i);
        break;
        case R.id.Capture : i = new Intent(this, Capture.class);
        startActivity(i);
        break;
        case R.id.About : i = new Intent(this, About.class);
        startActivity(i);
        break;
        case R.id.Feedback : i = new Intent(this, Feedback.class);
        startActivity(i);
        break;
        case R.id.Ig : i = new Intent(this, Instagram.class);
        startActivity(i);
        break;
        case R.id.Help : i = new Intent(this, Help.class);
        startActivity(i);
        break;
        default:break;
    }
}
}

这是我的 xml 文件: activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".Dashboard">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="230dp"
                android:orientation="vertical"
                android:gravity="center"
                android:background="#6A287E">

                <ImageView
                    android:layout_width="90dp"
                    android:layout_height="90dp"
                    android:src="@drawable/iconfyp"
                    android:layout_gravity="center"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="DASHBOARD"
                    android:textColor="@color/colorAccent"
                    android:layout_gravity="center"
                    android:textStyle="bold"
                    android:textSize="18sp"
                    android:layout_marginTop="10dp"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="215dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="135dp"
                    android:orientation="horizontal"
                    android:layout_marginBottom="10dp">

                    <androidx.cardview.widget.CardView
                        android:foreground="?android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true”
                        android:id="@+id/Login"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView1"
                                android:layout_height="50dp"
                                android:src="@drawable/people"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="LOGIN"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView1"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:foreground="?android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true”
                        android:id="@+id/Capture"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginLeft="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView2"
                                android:layout_height="50dp"
                                android:src="@drawable/cameraa"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="CAPTURE"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView2"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="135dp"
                    android:orientation="horizontal"
                    android:layout_marginBottom="10dp">

                    <androidx.cardview.widget.CardView
                        android:foreground="?android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true”
                        android:id="@+id/About"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView3"
                                android:layout_height="50dp"
                                android:src="@drawable/aboutt"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="ABOUT"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView3"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:foreground="?android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true”
                        android:id="@+id/Feedback"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginLeft="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView4"
                                android:layout_height="50dp"
                                android:src="@drawable/feedback"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="FEEDBACK "
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView4"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="135dp"
                    android:orientation="horizontal"
                    android:layout_marginBottom="10dp">

                    <androidx.cardview.widget.CardView
                        android:foreground="?android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true”
                        android:id="@+id/Ig"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView5"
                                android:layout_height="50dp"
                                android:src="@drawable/instagram"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="INSTAGRAM"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView5"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:foreground="?android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true”
                        android:id="@+id/Help"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginLeft="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView6"
                                android:layout_height="50dp"
                                android:src="@drawable/help"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="HELP"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView6"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                    android:background="@android:color/transparent"/>

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

最佳答案

添加 android:clickable="true"android:focusable="true"android:foreground="?android:attr/selectableItemBackground"

                   <androidx.cardview.widget.CardView
                    android:foreground="?android:attr/selectableItemBackground"
                    android:focusable="true
                    android:clickable="true"
                    android:id="@+id/Feedback"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_marginLeft="5dp"
                    android:orientation="vertical">

                 // your code
                   </androidx.cardview.widget.CardView>

关于java - 为什么当我单击按钮时我的页面不存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57645078/

相关文章:

javascript - 泛化 XPath

java - 将 xml 转换为另一个 xml 的最简单方法是什么?

java - SimpleMessageListenerContainer 批量消息处理

java - 使用 Selenium 在 Firefox 和 Chrome 中打开新标签页不起作用

android - 改造 2 : responseBodyConverter converts to null object

php - 使用 php 将特殊字符转换为 ISO Latin-1 代码?

java - Jersey 资源类的托管 Bean

java - Java Swing 中的 MouseMotionListener,将其与组件内的组件一起使用等

android - 如何在 Flutter 中处理 Android 设备的 BACK 按钮按下?

android - 如何将工作人员参数传递给 WorkManager 类