1
  1. My apps unfortunately stopped after right I put the OnClickListener
  2. My problem shows : at com.example.whatisthat.Dashboard.onCreate(Dashboard.java:23)
  3. Error : LoginBtn.setOnClickListener(this);

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_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>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Help"
                android:label="@string/Help"/>
    <activity android:name=".Instagram"
                 android:label="@string/Instagram"/>
    <activity android:name=".Feedback"
                 android:label="@string/Feedback"/>
    <activity android:name=".About"
        android:label="@string/About"/>
    <activity android:name=".Capture"
        android:label="@string/Capture"/>
    <activity android:name=".Login1"
        android:label="@string/Login1"/>
    <activity android:name=".Dashboard"
        android:label="@string/Dashboard"/>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1
  • You forgot to call setContentView(activity_dashboard); before the first findViewById() call.
    – Mike M.
    Aug 24, 2019 at 18:15

1 Answer 1

2

In your onCreate() method there isn't

setContentView(R.layout.activity_dashboard);

before the methods findViewById() which return null.

Use:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);   //<-- add the method

    LoginBtn = findViewById(R.id.Login);
1
  • 1
    That solved my problem. But why my page that i create inside the button cannot click?
    – wany arash
    Aug 24, 2019 at 18:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.