英文:
My own Android App causes my phone to crash
问题
以下是您提供的代码部分的翻译:
public class MainActivity extends AppCompatActivity {
String CHANNEL1_ID = "通道1标识";
String CHANNEL1_NAME = "通道1";
String channel1_description = "通道1描述";
String textTitle = "这是一个标题。";
String textContent = "一些内容。";
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 是您必须定义的每个通知的唯一整数
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);
}
}
}
英文:
I am trying to learn how to trigger notifications on Android. The app works on the virtual device but when I try it on my actual Samsung Galaxy S7 (Android Version: 8.1.0), it freezes the phone everytime I press the notification button with a message that says "System UI has stopped" which makes my phone unresponsive indefinitely and requires restarting.
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);
}
}
}
答案1
得分: 0
我只需要将以下内容添加到我的模块级build.gradle文件中:
implementation "com.android.support:support-compat:29.0.0"
因为我的目标SDK版本是29。
英文:
All I had to do was add the following to my module level build.gradle
implementation "com.android.support:support-compat:29.0.0"
since my target sdk version is 29.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论