我在 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() 调用时,按钮上的文本在 populate() 方法中没有改变。如果我从外部调用相同的方法,则一切正常。谁能帮我解决这个问题。