为什么我的应用程序突然停止? 不幸的是,在我将OnClickListener 我的问题是: 位于com.example.whatissthat.Dashboard.onCreate(Dashboard.java:23) 错误: LoginBtn.setOnClickListener(此)

为什么我的应用程序突然停止? 不幸的是,在我将OnClickListener 我的问题是: 位于com.example.whatissthat.Dashboard.onCreate(Dashboard.java:23) 错误: LoginBtn.setOnClickListener(此),java,android,crash,dalvik,Java,Android,Crash,Dalvik,Dashboard.java package com.example.whatisthat; import androidx.appcompat.app.AppCompatActivity; import androidx.cardview.widget.CardView; import android.content.Intent; import android.os.Bundle; import android.view.View; public class Dashboard ext

Dashboard.java

package com.example.whatisthat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

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

public class Dashboard extends AppCompatActivity implements     View.OnClickListener
{

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

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //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;
        }
    }
}
activity\u 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: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:id="@+id/buttonLogin"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        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:id="@+id/buttonCapture"
                                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: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:id="@+id/buttonAbout"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        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:id="@+id/buttonFeedback"
                                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: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:id="@+id/buttonInstagram"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        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:id="@+id/buttonHelp"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>

AndroidManifest.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: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:id="@+id/buttonLogin"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        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:id="@+id/buttonCapture"
                                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: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:id="@+id/buttonAbout"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        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:id="@+id/buttonFeedback"
                                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: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:id="@+id/buttonInstagram"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        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:id="@+id/buttonHelp"
                                android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>


在您的
onCreate()
方法中,不是

setContentView(R.layout.activity_dashboard);
before返回
null
的方法
findViewById()

使用:

@覆盖
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_dashboard);//在第一次调用
findViewById()
之前,您忘记调用
setContentView(activity_dashboard);
。这解决了我的问题。但为什么我在按钮内创建的页面无法单击?