本文介绍了在片段按钮点击时无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在片段的按钮,该按钮,我不得不重写的onClick()方法,但它不工作。
我有一个Taost和日志也正在单击按钮时。
I have a button in fragment, and for that button i had override onClick() method, but its not working.i have a Taost and a Log too when the button is being clicked.
public class DataShown extends Fragment implements OnClickListener{
Button tv;
TextView textview;
Activity activity=getActivity();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("3", "started");
View rootView = inflater.inflate(R.layout.datashown, container, false);
Log.d("3", "closed");
textview=(TextView) rootView.findViewById(R.id.textView1);
tv = (Button) rootView.findViewById(R.id.configButton);
tv.setOnClickListener((OnClickListener) activity);
return rootView;
}//onCtreate
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick","1");
Toast.makeText(activity, "on click", Toast.LENGTH_SHORT).show();
}
}
为什么它发生,我不知道,可能是其愚蠢的错误。
现在,敬酒是显示错误,则接受上下文对象,所以我提供的活动,不工作。
why its happening, i have no idea, may be its silly mistake.Now the toast is showing error, is accepts context object so i provided activity, not working.
推荐答案
修改
tv.setOnClickListener((OnClickListener) activity);
到
tv.setOnClickListener(this);
和的onClick
方法变更
Toast.makeText(activity, "on click", Toast.LENGTH_SHORT).show();
到
Toast.makeText(getActivity(), "on click", Toast.LENGTH_SHORT).show();
和删除
Activity activity=getActivity();
因为你做抛砖引玉吧片段连接到活动之前。
because you did initiate it before fragment attached to activity.
这篇关于在片段按钮点击时无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!