Home > front end >  notification channels app crashes and stops
notification channels app crashes and stops

Time:05-30

this is my code:

package com.rockykhan.notificationchannels;

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

public class app extends Application {

    // MAKING CHANNEL ID'S AS FINAL STRINGS
    public static final String CHANNEL_1_ID = "channel1";
    public static final String CHANNEL_2_ID = "channel2";

    @Override
    public void onCreate() {
        super.onCreate();

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

                NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH);
                NotificationChannel channel2 = new NotificationChannel(CHANNEL_2_ID, "Channel 2", NotificationManager.IMPORTANCE_LOW);

                channel1.setDescription("this is channel 1");
                channel2.setDescription("this is channel 2");

                NotificationManager manager = getSystemService(NotificationManager.class);
                manager.createNotificationChannel(channel1);
                manager.createNotificationChannel(channel2);
        }

    }
}

mainActivity:

package com.rockykhan.notificationchannels;

import static com.rockykhan.notificationchannels.app.CHANNEL_1_ID;
import static com.rockykhan.notificationchannels.app.CHANNEL_2_ID;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.Notification;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText title, text;
    Button btn_c1, btn_c2;
    private NotificationManagerCompat notificationManager;

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

        title.findViewById(R.id.notificationTitleText);
        text.findViewById(R.id.notificationMessageText);
        btn_c1.findViewById(R.id.sendNotificationThroughChannel1);
        btn_c2.findViewById(R.id.sendNotificationThroughChannel2);

        String notificationTitle = title.getText().toString();
        String notificationText = text.getText().toString();

        notificationManager = NotificationManagerCompat.from(this);

        btn_c1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_1_ID)
                        .setSmallIcon(R.drawable.imp_noti)
                        .setContentTitle(notificationTitle)
                        .setContentText(notificationText)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                        .build();

                notificationManager.notify(1, notification);

            }
        });
        btn_c2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_2_ID)
                        .setSmallIcon(R.drawable.noti)
                        .setContentTitle(notificationTitle)
                        .setContentText(notificationText)
                        .setPriority(NotificationCompat.PRIORITY_LOW)
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                        .build();

                notificationManager.notify(2, notification);

            }
        });

    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <Button
        android:id="@ id/sendNotificationThroughChannel2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send on channel 2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@ id/notificationTitleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Title"
        android:inputType="textPersonName"
        android:minHeight="48dp"
        app:layout_constraintBottom_toTopOf="@ id/sendNotificationThroughChannel2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@ id/sendNotificationThroughChannel1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send on channel 1"
        app:layout_constraintBottom_toTopOf="@ id/sendNotificationThroughChannel2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/notificationMessageText" />

    <EditText
        android:id="@ id/notificationMessageText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Text"
        android:inputType="textPersonName"
        android:minHeight="48dp"
        app:layout_constraintBottom_toTopOf="@ id/sendNotificationThroughChannel1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/notificationTitleText" />
</androidx.constraintlayout.widget.ConstraintLayout>

logcat:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rockykhan.notificationchannels/com.rockykhan.notificationchannels.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.EditText.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3408)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3547)

So I was trying to send notifications through buttons but the app doesn't seem to start. It crashes and as far as I thought, it was a problem in xml Id's but I can't find the main problem here ( I think it is something to do with the findViewbyId of the editText). Help will be much appreciated!

CodePudding user response:

Assign the result of calling findViewById() to your View variables, don't call findViewById() on the variables itself.

So basically replace these lines

title.findViewById(R.id.notificationTitleText);
text.findViewById(R.id.notificationMessageText);
btn_c1.findViewById(R.id.sendNotificationThroughChannel1);
btn_c2.findViewById(R.id.sendNotificationThroughChannel2);

with these

title = findViewById(R.id.notificationTitleText);
text = findViewById(R.id.notificationMessageText);
btn_c1 = findViewById(R.id.sendNotificationThroughChannel1);
btn_c2 = findViewById(R.id.sendNotificationThroughChannel2);
  • Related