关于findViewById返回空指针的错误

缓行者 2013-01-23 02:50:38
今天遇到一个错误,百度了一下没能解决问题,也许搜索能力有待提高
先把部分代码贴出来
protected void onCreate(Bundle savedInstanceState) {

Log.v("GameWindow", "onCreate");

super.onCreate(savedInstanceState);
setContentView(R.layout.gameview);

// LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
// View layout = inflater.inflate(R.layout.gameview,null);

// gameView = (GameView) view.findViewById(R.id.game_sudo_view);

gameView = (GameView) findViewById(R.id.game_sudo_view);
if(gameView == null){
Log.v("gameView", "onCreate->null");
}

levelTextView = (TextView)findViewById(R.id.leveltextview);
levelTextView.setText("Topic."+1));

if(levelTextView == null){
Log.v("TextView", "onCreate->null");
}
}

下面是gameview.xml
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/back">

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:contentDescription="@string/app_name"
android:src="@drawable/grass" />

<com.ljx.gameview.GameView
android:id="@+id/game_sudo_view"
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<TextView
android:layout_centerHorizontal="true"
android:layout_above="@id/game_sudo_view"
android:id="@+id/leveltextview"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout>

现在问题是,同样在一个布局文件中,textView可以通过findViewById获取,但自定义组件com.ljx.gameview.GameView总是获得空指针
求各路前辈支援,解答
...全文
866 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
cc379958 2014-09-20
  • 打赏
  • 举报
回复
果然 啊,比如说你要获取viewpager下某个页面中的button, 你得先用LayoutInflater inflater = LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.gameview,null); 这种方式获取到这个页面,然后用view.findviewbyid才能找到这个button 否则会报空指针异常。
404bugs 2014-09-16
  • 打赏
  • 举报
回复
和我的问题一样,楼主怎么解决的?
softneo 2014-05-06
  • 打赏
  • 举报
回复
唉,bug漫天飞啊!
lineco 2013-04-08
  • 打赏
  • 举报
回复
楼主、怎么解决的啊、是用动态创建控件吗?
Maple_枫 2013-04-08
  • 打赏
  • 举报
回复
如果直接在activity里面使用findViewById(),完整的形式是this.findViewById().所以想获取另外xml的控件,通过获取的view来调用findViewById,例如(我的主布局是main.xml,要获取myview.xml的imageview)
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.myview, null);
        ImageView view1=(ImageView)view.findViewById(R.id.imageview);
这样之后就可以获取到正确的控件了
缓行者 2013-01-23
  • 打赏
  • 举报
回复
暂时用oyq_yangy的方法解决了需求
  • 打赏
  • 举报
回复
我觉得是哪个自定义控件的问题,但我不知道问题具体在什么地方
oyq_yangy 2013-01-23
  • 打赏
  • 举报
回复
哦,那就百度“android动态创建控件”
缓行者 2013-01-23
  • 打赏
  • 举报
回复
其实是我没能理解动态创建的意思,我是新手
oyq_yangy 2013-01-23
  • 打赏
  • 举报
回复
,如果不愿意动态创建的话,我也不知道了。。。等等看看还有没有其它人有过类似的经验吧
缓行者 2013-01-23
  • 打赏
  • 举报
回复
我试过这样引用
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);     
View view = inflater.inflate(R.layout.gameview,null);
gameView = (GameView) view.findViewById(R.id.game_sudo_view);
也试过clean项目了,但不知怎么解决
缓行者 2013-01-23
  • 打赏
  • 举报
回复
有代码,有点长,我简略贴出来
public class GameView extends View{
	
        ……
	public GameView(Context context,AttributeSet a){
	       ……
	}
	
	//自定义view的宽高
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = game.getScreenWidth();
		int height = game.getScreenHeight()-game.getScreenHeight()/4;
		setMeasuredDimension(width, height);
		//自定义view的宽高时,不实用下面函数
		//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	 
	@SuppressLint("DrawAllocation") @Override
	protected void onDraw(Canvas canvas) {
		……
		super.onDraw(canvas);
	}
	……
}

oyq_yangy 2013-01-23
  • 打赏
  • 举报
回复
findViewByid是在Activity自己的window上查找的。。。 你有那个view的代码吗? 不行就自己动态在代码里创建view好了
缓行者 2013-01-23
  • 打赏
  • 举报
回复
什么意思,解释下
oyq_yangy 2013-01-23
  • 打赏
  • 举报
回复
或许这个view自己弄了个window去显示。。。
缓行者 2013-01-23
  • 打赏
  • 举报
回复
可以正常显示的,我本来是想获取那个id然后在线程里更新view的,但是一直都是空指针
oyq_yangy 2013-01-23
  • 打赏
  • 举报
回复
也是,没有就编译有问题了。 你的那个view能正确显示吗?
缓行者 2013-01-23
  • 打赏
  • 举报
回复
public static final class id {
        ……
        public static final int gallery01=0x7f080018;
        public static final int game_sudo_view=0x7f080011;
        public static final int gamegridview=0x7f080010;
        public static final int grid_image=0x7f080013;
        public static final int grid_text=0x7f080014;
        public static final int leveltextview=0x7f080012;
        ……
    }
缓行者 2013-01-23
  • 打赏
  • 举报
回复
有的
public static final class id {
        ……
        public static final int gallery01=0x7f080018;
        public static final int game_sudo_view=0x7f080011;
        public static final int gamegridview=0x7f080010;
        public static final int grid_image=0x7f080013;
        public static final int grid_text=0x7f080014;
        public static final int leveltextview=0x7f080012;
        ……
    }
oyq_yangy 2013-01-23
  • 打赏
  • 举报
回复
看看生成的R.java里有game_sudo_view吗?
加载更多回复(1)

80,470

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧