gpt4 book ai didi

java - 我自己的 Android 应用程序导致我的手机崩溃

转载 作者:行者123 更新时间:2023-12-01 17:57:38 26 4
gpt4 key购买 nike

我正在尝试学习如何在 Android 上触发通知。该应用程序可以在虚拟设备上运行,但当我在实际的三星 Galaxy S7(Android 版本:8.1.0)上尝试它时,每次我按下通知按钮时,手机都会卡住,并显示一条消息“系统 UI 已停止”,这使得我的手机无限期无响应,需要重新启动。

public class MainActivity extends AppCompatActivity {

String CHANNEL1_ID = "Channel 1 id";
String CHANNEL1_NAME = "Channel 1";
String channel1_description = "Channel 1 description";
String textTitle = "This is a title.";
String textContent = "Some content.";
int count = 0;

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

Button button = (Button) findViewById(R.id.button);
final Context context = this;

createNotificationChannel();

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

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL1_ID);
builder.setSmallIcon(R.drawable.notification_image);
builder.setContentTitle(textTitle);
builder.setContentText(textContent);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

// notificationId is a unique int for each notification that you must define
notificationManager.notify(count, builder.build());

count++;

Log.d("TAG " + count, "onClick: ");

}

});

}

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = CHANNEL1_NAME;
String description = channel1_description;
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL1_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}

}

最佳答案

我所要做的就是将以下内容添加到我的模块级别 build.gradle 中实现“com.android.support:support-compat:29.0.0”因为我的目标 sdk 版本是 29。

关于java - 我自己的 Android 应用程序导致我的手机崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60688021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com