我的应用程序中有一个子片段,我试图测试功能。想法是textview将更新以模仿输入edittext的内容。但是目前它并不喜欢setText,我不知道为什么。我已经检查过我是否错过了导入,我认为小部件是正确定义的。关于如何解决这个问题的任何建议都会很棒。
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class WeightFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_weight, container, false);
return rootView;
}
EditText weight = (EditText) getActivity().findViewById(R.id.weightInput);
TextView label = (TextView) getActivity().findViewById(R.id.weightResult);
String result = weight.getText().toString();
label.setText(result);
}
答案 0 :(得分:0)
将代码INSIDE放在一个方法中,如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_weight, container, false);
EditText weight = (EditText) getActivity().findViewById(R.id.weightInput);
TextView label = (TextView) getActivity().findViewById(R.id.weightResult);
String result = weight.getText().toString();
label.setText(result);
return rootView;
}