安卓开发常见异常错误

问题一: in layout has no declaration in the base layout folder

在Android Studio的 File 里有一个 Invalidate Caches / Restart 点击它再从弹出窗里选择 Invalidate and Restart.

Android Studio重启后正常。问题解决。

问题二:解决组件化开发butterknife 在 library中使用的坑 

https://blog.csdn.net/zyw0101/article/details/80399225   组件化开发butterknife 在 library中使用

java.lang.BootstrapMethodError: Exception from call site #0 bootstrap method

    ButterKnife requires that you enable Java 8 in your builds to function as of version 9.0.0 and newer. You can learn more about     how to enable this at https://developer.android.com/studio/write/java8-support.

  Butterknife 9.0.0以后的版本需要Java8的支持。

  在build.gradle中加入:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

  问题三:android icon  放在mipmap中显示不出来

  直接删除选中的文件夹,有相关报错直接删除对应文件

 

  问题四:android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

 解决方法:加入通知  

public class DataGetterService extends Service {

    private static final int ROLL_MESSAGE = 1001;

    private static final long DELAY_TIME = 10000;

    PowerManager.WakeLock wl;

    private MainPresenter mPresenter ;

    private Handler rollHandler = new Handler() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == ROLL_MESSAGE) {
                mPresenter.getData();
                rollHandler.sendEmptyMessageDelayed(ROLL_MESSAGE,DELAY_TIME);
            }

        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel cn = new NotificationChannel("40", "datagetterservice", NotificationManager.IMPORTANCE_DEFAULT);
            cn.enableLights(false);
            cn.enableVibration(false);
            cn.setSound(null, null);
            nm.createNotificationChannel(cn);
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "40");
        builder.setVibrate(new long[]{0}).setSound(null);
        startForeground(2, builder.build());
        mPresenter = new MainPresenter();
        rollHandler.sendEmptyMessage(ROLL_MESSAGE);
        wl = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "getdata");
        wl.acquire();
        Log.v("dawn","oncreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v("dawn","onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if (rollHandler != null) {
            rollHandler.removeMessages(ROLL_MESSAGE);
        }

        if (mPresenter!=null) {
            mPresenter.onUnsubscribe();
        }
        stopForeground(true);
        Log.i("dawn", "onDestroy: 服务关闭");
        if (wl != null) {
            wl.release();
        }
        super.onDestroy();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值