第一行代码(第二版)运行BUG小结

这篇博客总结了《第一行代码》第二版在Android开发中遇到的诸多问题,包括Android Studio引入问题、主题设置错误、百分比布局库依赖、RecyclerView引入、通知渠道设置、模拟器权限、网络访问限制、数据库操作、广播接收器使用、内容提供器创建等,并提供了相应的解决方案。
摘要由CSDN通过智能技术生成

官方文档

官方文档

Android Studio引入

C:\Program Files\Android\Android Studio\jre\bin
C:\Users\admin\AppData\Local\Android\Sdk\platform-tools

初始运行时报错

版本过高,将app\build.gradle版本降低

//将 33版本改为30
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"
    defaultConfig {
        targetSdkVersion 30
    }
}

Android主题设置为@android:style/Theme.Dialog报错

用@android:style/Theme.Dialog的话必须把对应的JAVA程序继承的 AppCompatActivity改为Activity
如果使用@style/Theme.AppCompat.Dialog的话就不用改
原因:
eclipse的MainActivity.java继承的是Activity类,而android studio中Main继承的是AppCompatActivity

添加百分比布局库依赖问题

版本过老,Android studio升级到3.0之后,compile就改为了implementation
依赖的写法改为:implementation

implementation 'androidx.percentlayout:percentlayout:1.0.0'

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:text="Button 1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
    <Button
        android:id="@+id/button2"
        android:text="Button 2"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
    <Button
        android:id="@+id/button3"
        android:text="Button 3"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
    <Button
        android:id="@+id/button4"
        android:text="Button 4"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
</androidx.percentlayout.widget.PercentFrameLayout>

RecyclerView的引入问题

app/build.gradle,引入

implementation 'com.android.support:recyclerview-v7:30.1.0'

在gradle.properties中有下面两行代码:

android.useAndroidX=true//当前项目是否启用androidx
android.enableJetifier=true//是否将依赖包迁移至androidx。

因此新建布局时需要以androidx为路径的开头,修改布局代码如下:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

message.png

链接: message_left.png

在这里插入图片描述

制作Nine-Patch 环境配置及使用

链接: 环境配置

广播接收器的时候,监听网络变化,告知用户是有网络还是无网络,出错

//connectionManager.getActiveNetworkInfo()为红色,是因为没有权限
NetworkInfo networkInfo=connectionManager.getActiveNetworkInfo();

在app/src/main/AndroidManifest.xml里添加权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

接收系统广播 静态注册实现开机启动

需要注意一下,不是看到启动界面以后就是启动完成了,而是得等他全部准备好,才会出现提示框,显示“Boot Complete”,就好像开了电脑以后,得等那个电脑管家提示“开机用了多少秒,打败了全国百分之多少的用户”,才差不多算完全开机

自定义广播无法显示

在安卓8.0之后取消了大部分静态注册

方法一:该方法可以实现标准广播和有序广播,还可截断有序广播

发送广播的时候携带intent.addFlags(0x01000000); 即能让广播突破隐式广播限制。

        Button button=(Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
                //发送广播前加上“intent.addFlags(0x01000000);”骗过安卓8.0+的检测,即能让广播突破隐式广播限制
                intent.addFlags(0x01000000);
                
                sendBroadcast(intent);//标准广播
                //sendOrderedBroadcast(intent,null);//有序广播,截断有序广播
            }
        });

截断有序广播
Broadcasttest项目—>MainActivity.java

        Button button=(Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
                //发送广播前加上“intent.addFlags(0x01000000);”骗过安卓8.0+的检测,即能让广播突破隐式广播限制
                intent.addFlags(0x01000000)
                sendOrderedBroadcast(intent,null);
            }
        });

Broadcasttest项目—>AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.broadcasttest">
    ...
        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true"
            >
            //通过android:priority设置优先级
            <intent-filter  android:priority="100" >
                <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
            </intent-filter>
        </receiver>
        ...
</manifest>

Broadcasttest项目—>My

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值