2013-01-16 57 views
-3
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.myfragment, container,false); 
    ImageView tv = (ImageView) v.findViewById(R.id.imageView); 
    tv.setImageResource(R.drawable.h1); 
    return tv; 
} 

为什么我得到运行时错误?孩子已经有父错误显示?

+1

return v?你是这个意思吗? – Leonidos

+2

请包括更多信息,你会得到帮助。 *确切*你得到什么错误?包含堆栈跟踪。 (猜猜,返回v,而不是电视,但不可能告诉。) –

+0

谢谢..这是我的一个愚蠢的错误。你是正确的,它应该是“返回v” – Lovy

回答

0

如下更改上面的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.myfragment, container,false); 
    ImageView tv = (ImageView) v.findViewById(R.id.imageView); 
    tv.setImageResource(R.drawable.h1); 
    return v; 
} 

您得到RuntimeError的原因是你是返回ImageView(TV)为onCreateView这是视图Fragment。但是ImageView是一个小孩查看myfragment的布局,所以它不能设置为ViewFragment所以你得到运行时错误。

+0

你真的改变了什么吗? – njzk2

+0

@ njzk2这是一个错误,现在编辑完成 – TNR

相关问题