【发布时间】:2015-04-18 09:46:23
【问题描述】:
我正在做一个导航抽屉应用程序。我的所有案例都在工作,我的意思是,当触摸某个部分时,应用程序会转到正确的视图。但是当我想开始编码时,我不知道如何在视图中使用方法。我认为我的代码会比我解释得更好:
public class News extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setText();
return inflater.inflate(R.layout.lay_news, container, false);
}
public void setText(){
TextView texts = (TextView) getView().findViewById(R.id.textView);
texto.setText("hi");
}
}
NullPointerException 当我调用 setText 方法时。我知道这是 TextView 的定义,但我不知道如何在没有 getView() 或 getActivty() 的布局中“找到”它,当视图加载时都返回 NullPointerException。
【问题讨论】:
标签: android android-fragments view nullpointerexception navigation-drawer