GridView Note

GridViewNote笔记

1. android.widget.GridLayout cannot be cast to android.widget.GridView

看一下布局文件,八成是GridView写错了

我就是写成了GridLayout

2. 解决:Attempt to invoke virtual method ‘void android.widget.GridView.setAdapter(android.widget.ListAdapter)’ on a null object reference

在使用GirdView和ListView时经常会报这个错,仔细看错误信息发现是 NullPointerException,debug发现GirdView为null。

为什么会报这个错,原因是你使用的是this.findViewById()来取得GirdView,但事实是这样获取不到该对象。

所以正确的做法是:

使用布局填充器LayoutInflater的infalte方法。

View view= this.getLayoutInflater().inflate((GirdView 所在layout文件的资源id,例:R.layout.main), null);
GirdView gridView = view.findViewById(R.id.gridview );

3. Android中如何截取字符串中某个字符之前或之后的字符串

//截取#之前的字符串
String str = "sdfs#d";
str.substring(0, str.indexOf("#"));

//截取之后的字符
String userId = "21321321?u=2132132132";
String userIdJiequ = userId.substring(userId.indexOf("?u="));

发布了83 篇原创文章 · 获赞 37 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xbean1028/article/details/104762299