英文:
onClickListener for a button not working in a fragment
问题
这是我的代码,我不知道为什么它不监听按钮事件。
下面的代码来自片段文件:
// private Button button;
LayoutInflater lf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lf=inflater;
View view = inflater.inflate(R.layout.fragment_home,
container, false);
Button button = (Button) view.findViewById(R.id.home_button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent=new Intent(lf.getContext(),sunhan_activity.class);
startActivity(intent);
}
});
return inflater.inflate(R.layout.fragment_home, container, false);
}
英文:
this is my code and I don't know why it is not listening for the button events
the below code is from fragment file
// private Button button;
LayoutInflater lf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lf=inflater;
View view = inflater.inflate(R.layout.fragment_home,
container, false);
Button button = (Button) view.findViewById(R.id.home_button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent=new Intent(lf.getContext(),sunhan_activity.class);
startActivity(intent);
}
});
return inflater.inflate(R.layout.fragment_home, container, false);
}
答案1
得分: 2
看看你的代码,你就会明白为什么代码不起作用。实际上,你膨胀(inflate)了布局两次,所以最后一个将被执行,而你第一次膨胀的View view = inflater.inflate(R.layout.fragment_home, container, false);
将不会被执行,因为你没有使用它。
只需像这样更改代码:
// private Button button;
LayoutInflater lf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lf = inflater;
View view = inflater.inflate(R.layout.fragment_home,
container, false);
Button button = (Button) view.findViewById(R.id.home_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(lf.getContext(), sunhan_activity.class);
startActivity(intent);
}
});
return view;
}
英文:
Look at your code and you will get idea why the code is not working. Actually you are inflating layout 2 times, so last one will be executed and your first inflate View view = inflater.inflate(R.layout.fragment_home,container, false);
will not be executed as you have not used it.
Simply change code like this:
// private Button button;
LayoutInflater lf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lf=inflater;
View view = inflater.inflate(R.layout.fragment_home,
container, false);
Button button = (Button) view.findViewById(R.id.home_button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent=new Intent(lf.getContext(),sunhan_activity.class);
startActivity(intent);
}
});
return view;
}
答案2
得分: 1
-
你不需要为
LayoutInflater
声明额外的变量,因为你的onCreateView()
已经有一个可用的。 -
在移除后,不要再次从inflater调用context,你的fragment有自己的context。所以在intent中用
getContext()
替换lf.getContext()
。由于你从fragment中调用了一个activity,所以使用getActivity()
而不是getContext()
。 -
最后不要再次返回新的
inflater
,你之前已经分配了一个给View
。所以返回你从view中声明的变量。
将这个:
return inflater.inflate(R.layout.fragment_home, container, false);
替换为:
return view;
最终代码应该如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Button button = (Button) view.findViewById(R.id.home_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), sunhan_activity.class);
startActivity(intent);
}
});
return view;
}
英文:
-
You don't have to declare extra variable for
LayoutInflater
because youronCreateView()
already has one to use. -
Don't call context from inflater again after removing, your fragment has it's own context. So replace
lf.getContext()
withgetContext()
inside intent. Since you are calling an activity from fragment usegetActivity()
instead ofgetContext()
. -
Don't return new
inflater
again at the end, you have previously assigned one before toView
. So return the varibale you have declared from view.
replace this
return inflater.inflate(R.layout.fragment_home, container, false);
with
return view;
Finally this is how it should look like:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Button button = (Button) view.findViewById(R.id.home_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), sunhan_activity.class);
startActivity(intent);
}
});
return view;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论