无法从onActivityResult访问视图() [英] Not able to access view from onActivityResult()
问题描述
我在片段中的以下情形:
I have the following scenario in Fragment :
onCreateView(){
view = inflater.inflate(R.layout.mylayout, container, false);
ScrollView sc = (scrollView)view.findViewById(R.id.sc);
generateUI()
return view; }
public void generateUI() {
horizontalllayout = new LinearLayout(getActivity());
horizontalllayout.setOrientation(android.widget.LinearLayout.HORIZONTAL);
Button newbtn = new Button(getActivity());
newbtn.setId(1);
horizontalllayout.addView(newbtn);
newbtn.setOnClickListener(
public void onClick(View v) {
Intent CaputureIntent = new Intent(getActivity(), s.class);
startActivityForResult(CaputureIntent, 664);
} });
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == 1666){
populate("hi");
}
}
public void populate(String str) {
Button b = (Button)getActivity().findViewbyId(1); //Error
b.setText(str);
}
}
我不能够接受的范围内。按钮上的文字不是填充()方法改变了,当我打电话从onActivityResult()。如果我从每一件事情外调用相同的方法工作正常。谁能帮我在设法解决这个问题。
I am not able to receive the context. The text on the button is not changed in populate() method , when I call from onActivityResult(). If I call the same method from outside every thing is working fine. Can anyone help me in sorting out this issue.
推荐答案
假设按钮是deined布局文件中,你的code是通过定义b键入正确,B组是通过整体的code访问在你的类:
assuming Button is deined inside your layout file and your code is typed correctly, set b to be accessible through whole your code by defining b in your class:
Button b;
,然后里面onCreateView(接入B)是这样的:
and then access b inside onCreateView() like this:
b = (Button)view .findViewbyId(1);
里面onActivityResult只是采用B但是你喜欢它:
inside onActivityResult just use b however you like it:
b.setText(str);
这篇关于无法从onActivityResult访问视图()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!