Ashleigh1806 2020-11-06 00:01 采纳率: 0%
浏览 447

为什么我的Android studio程序运行不出来

主java:

package com.example.administrator.myapplication932;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;

public class MainActivity extends Activity {
    final int NOTIFYID=0x123;
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final NotificationManager notificationManager=
                (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder notification=new Notification.Builder(this);
        notification.setAutoCancel(true);
        notification.setSmallIcon(R.drawable.packet);
        notification.setContentTitle("奖励百万红包!!!");
        notification.setContentText("点击查看详情!");
        notification.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE);
        notification.setWhen(System.currentTimeMillis());
        Intent intent=new Intent(MainActivity.this, Detail.class);
        PendingIntent pi=PendingIntent.getActivity(MainActivity.this,0,intent,0);
        notification.setContentIntent(pi);
            notificationManager.notify(NOTIFYID,notification.build());
    }
}

主活动:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@drawable/background"
    tools:context=".MainActivity">


</RelativeLayout>

detial java:

package com.example.administrator.myapplication932;

import android.app.Activity;
import android.os.Bundle;

public class Detail extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
    }
}

detial活动:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@drawable/img"
    tools:context=".Detail">

</RelativeLayout>

没报错

  • 写回答

1条回答 默认 最新

  • qq_41198616 2020-11-06 11:02
    关注

    你写的是一个通知吧,通知需要加载在管道上才能使用

    如果您以 Android 8.0(API 级别 26)为目标平台,并在未指定通知渠道的情况下发布了一条通知,则该通知不会显示且系统会记录错误

    如下面这种:

     private String createNotificationChannel(String channelID, String channelNAME, int level) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
                manager.createNotificationChannel(channel);
                return channelID;
            } else {
                return null;
            }
        }
    NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    String channel = createNotificationChannel("myChannelID", "myChannel", NotificationManager.IMPORTANCE_HIGH);
    Notification notification=new NotificationCompat.Builder(this,channel)
            .setContentTitle("测试通知,这是Title")
            .setContentText("这只一段文本,这是Text")
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)).build();
    manager.notify(1,notification);

     

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)