【Android Studio】初学者使用AS过程中遇到的碎片问题记录

学习AS过程中参考的教程列表

碎片问题

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。
    p1

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类未找到");
        }
    }
  • 问题及解决方法:
  1. 代码如果不写成try-catch,程序可能会在Class.forName找不到Class时闪退,因此这里建议用try-catch捕捉异常,方便调试,不建议用throws Exception的写法。
  2. 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条说的应该是解决办法:【点击查看】
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值