这里写自定义目录标题
学习AS过程中参考的教程列表
- 安装AS 2022.3.1时,参考教程:
Android Studio 安装配置教程 - Windows(详细版) - 首次编译下载十分缓慢,可以设置代理,参考教程:
Android Studio国内镜像总结以及彻底清除AS代理设置 - 初步了解AS的使用时,参考教程:
Android 教程
1.0 Android基础入门教程
Android 安卓教程
碎片问题
menu_main标红
- 代码:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
- 问题:
代码中的menu_main被标红,原因是缺少app/res/menu/menu_main.xml文件。 - 解决方法:
在AS中,鼠标悬停在标红处,会出现解决方案,点一下即可直接创建menu_main.xml文件。
或者自己右键res,New,Android Resource File,如下图填写内容后,OK。
Class.forName报异常
- 代码
// 广播自定义意图
public void broadcastIntent(View view){
try {
Intent intent = new Intent();
intent.setClass(this,Class.forName("com.example.helloworld.MyReceiver"));
sendBroadcast(intent);
}
catch(ClassNotFoundException e){
Log.d("MainActivity","com.example.helloworld.MyReceiver类未找到");
}
}
- 问题及解决方法:
- 代码如果不写成try-catch,程序可能会在Class.forName找不到Class时闪退,因此这里建议用try-catch捕捉异常,方便调试,不建议用throws Exception的写法。
- Class.forName()括号内应为字符串,字符串内容应为Package.Class。
如果程序没有达到预期效果,查看Logcat,发现报了"com.example.helloworld.MyReceiver类未找到",则考虑Package.Class是不是写错了,更正即可。
activity_main.xml中出现的问题
ImageButton无法居中
- 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
- 问题:
出现ImageButton无法居中,且其余组件也不能按照期待的位置摆放。 - 解决方法:
检查代码开头及结尾是否为<RelativeLayout…,如果不是修正即可。
ADV虚拟机中不存在“返回”键
- 问题:
想看“返回”操作报的log时,在ADV操作界面找不到“返回”键。 - 解决方法:
在ADV屏幕界面,会将鼠标操作模拟为对屏幕的操作,因此将鼠标放到屏幕左侧或右侧边缘,按住鼠标左键后向屏幕中间拖动,看到标志“返回”的箭头后松开鼠标,即可实现“返回”操作。
或按键盘“ESC”按键也可实现“返回”操作。
Failed to find provider
- 问题:
代码中使用了Provider,执行时程序闪退报错,后发现应该是Manifest.xml文件里,注册的provider里的authorities属性写错了,所以导致provider找不到。 - 参考:
点击查看链接
碎片知识
关键字final
在Java中声明属性、方法和类时,可使用关键字final来修饰。
- final变量即为常量,只能赋值一次;
- final方法不能被子类重写;
- final类不能被继承。
static final String PROVIDER_NAME = "com.example.provider.College";
query()方法
From: Android中关于SQLiteDataBase的query()方法参数详解
query(String table,String [] columns, String selection,String [] selectionArgs,String groupBy, String having, String orderBy,String limit)
各参数的含义:
table :表名。相当于select *** from table语句中的table 。如果是多表联合查询,可以用逗号将两个表名分开。
columns :要查询出来的列名。相当于 select *** from table语句中的 ***部分 。如果是查询多个参数,可以用逗号将两个表名分开。例:new String[]{“name”,“age”,“sex”}
selection:查询条件子句,相当于select *** from table where && 语句中的&&部分,在条件子句允许使用占位符“?”表示条件值 ,例:“name=?,age=?,sex=?”
selectionArgs :对应于 selection参数 占位符的值,值在数组中的位置与占位符在语句中的位置必须一致,否则就会有异常。 例:与 new String[]{“lisa”,“1”,“女”}
groupBy :相当于 select *** from table where && group by … 语句中 … 的部分 ,作用是:将同一列的相同名字的参数合并在一起 例;在name列有两个Jame(name:Jame --salay:100,name:Jame --salay:200),使用…group by name查询后 只显示一个Jame的集合(name:Jame–salay:300)
having :相当于 select *** from table where && group by …having %%% 语句中 %%% 的部分, 作用于groupBy的条件,例:havig name>2意思是name列相同参数>2
orderBy :相当于 select ***from ?? where&& group by …having %%% order by@@语句中的@@ 部分,如: personid desc(按person 降序), age asc(按age升序);
limit:指定偏移量和获取的记录数,相当于select语句limit关键字后面的部分。
获取数据库数据报错“Value must be ≥ 0 but getColumnIndex
can be -1”
Cursor Warning
- 问题:
- 解决方法:
文章中第2条说的应该是解决办法:【点击查看】