Android的 - 添加code到A片段 [英] Android - Add code to a fragment
问题描述
我是初学者编码为Android,我试图用两个片段做一个应用程序。不幸的是,当我添加code设置行动我的布局,它使我的应用程序崩溃,所以我不知道我应该把我的code的片段文件。如果我拿出功能的onCreate,应用程序不会崩溃,我的布局是好的。
下面是我的code。非常感谢你的回答。
公共类FragmentOne扩展片段{ 公共静态最后的字符串标记=FragmentOne;
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
视图V = View.inflate(getActivity(),R.layout.fragmentone,NULL); 返回伏;
} 公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
最终的EditText etData =(EditText上)getView()findViewById(R.id.etData)。
}
}
首先我可以看到你的$ C所有$ c.First为一些失误 @ user1873880
提到的,的onCreate()
在 onCreateView总是被称为()
,所以你应该考虑处理您的意见 onCreateView()
。第二个错误,我可以看到的是,你是不是创建查看
,因为它的设计是对片段
使用。在我看来,你的片断应该看的方式是这样的:
公共类FragmentOne扩展片段{ 私有静态最后弦乐TAG =FragmentOne; 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
super.onCreateView(充气器,容器,savedInstanceState); //创建一个使用LayoutInflater您的看法
返回inflater.inflate(R.layout.fragmentone,集装箱,FALSE);
} 公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
//做你的变量initialisations这里除了查看!
} 公共无效onViewCreated(查看视图,捆绑savedInstanceState){
super.onViewCreated(查看,savedInstanceState);
//初始化你的看法
的EditText etData =(EditText上)view.findViewById(R.id.etData);
}
}
希望这可以帮助你! :)
I am a beginner in coding for Android and i am trying to do an application with two fragments. Unfortunately, when i add code to set action to my layout, it makes my application crash, so i'm wondering where i should put my code on the fragment file. If i take out the function onCreate, the application doesn't crash and my layout is good.
Here is my code. Thank you so much for your answer.
public class FragmentOne extends Fragment{
public static final String TAG = "FragmentOne";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = View.inflate(getActivity(), R.layout.fragmentone, null);
return v;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final EditText etData = (EditText) getView().findViewById(R.id.etData);
}
}
First of all I can see a few mistakes in your code.First of all as @user1873880
mentioned, onCreate()
is always called before onCreateView()
, so you should consider dealing with your views in onCreateView()
. Second mistake which I can see is that you are not creating your View
as it's designed to be used on Fragment
. In my opinion the way your Fragment should look is like this :
public class FragmentOne extends Fragment {
private static final String TAG = "FragmentOne";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// create your view using LayoutInflater
return inflater.inflate(R.layout.fragmentone, container, false);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// do your variables initialisations here except Views!!!
}
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
// initialise your views
EditText etData = (EditText) view.findViewById(R.id.etData);
}
}
Hope this helps you! : )
这篇关于Android的 - 添加code到A片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!