通知渠道应用程序崩溃并停止
notification channels app crashes and stops
这是我的代码:
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);
}
}
}
主要活动:
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)
所以我试图通过按钮发送通知,但应用程序似乎没有启动。它崩溃了,据我所知,这是 xml Id 中的一个问题,但我在这里找不到主要问题(我认为这与 editText 的 findViewbyId 有关)。非常感谢您的帮助!
将调用 findViewById()
的结果分配给您的视图变量,不要对变量本身调用 findViewById()
。
所以基本上替换这些行
title.findViewById(R.id.notificationTitleText);
text.findViewById(R.id.notificationMessageText);
btn_c1.findViewById(R.id.sendNotificationThroughChannel1);
btn_c2.findViewById(R.id.sendNotificationThroughChannel2);
有了这些
title = findViewById(R.id.notificationTitleText);
text = findViewById(R.id.notificationMessageText);
btn_c1 = findViewById(R.id.sendNotificationThroughChannel1);
btn_c2 = findViewById(R.id.sendNotificationThroughChannel2);
这是我的代码:
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);
}
}
}
主要活动:
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)
所以我试图通过按钮发送通知,但应用程序似乎没有启动。它崩溃了,据我所知,这是 xml Id 中的一个问题,但我在这里找不到主要问题(我认为这与 editText 的 findViewbyId 有关)。非常感谢您的帮助!
将调用 findViewById()
的结果分配给您的视图变量,不要对变量本身调用 findViewById()
。
所以基本上替换这些行
title.findViewById(R.id.notificationTitleText);
text.findViewById(R.id.notificationMessageText);
btn_c1.findViewById(R.id.sendNotificationThroughChannel1);
btn_c2.findViewById(R.id.sendNotificationThroughChannel2);
有了这些
title = findViewById(R.id.notificationTitleText);
text = findViewById(R.id.notificationMessageText);
btn_c1 = findViewById(R.id.sendNotificationThroughChannel1);
btn_c2 = findViewById(R.id.sendNotificationThroughChannel2);