我的自己的Android应用导致我的手机崩溃。

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年3月15日 06:46:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/60688021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定