2

我正在尝试将 Activity 转换为片段。setContentView 上的错误标记。

这是我的代码

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_googlev2);
    Init();
    addMarkersToMap();
    yStart = 21.102918;
    yEnd = 20.960798;
    xStart = 105.772762;
    xEnd = 105.900650;
    xNow = xStart;
    yNow = yStart;
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line);
    textView = (AutoCompleteTextView) getView().findViewById(R.id.autoCompleteTextView1);
    textView.setThreshold(3);
    adapter.setNotifyOnChange(true);
    textView.setAdapter(adapter);
    textView.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count){
            autoCount = count;
            autoS = s;
            if(autoCount % 3 == 1) {
                stCommand = "AutoCompleteTextView";
                lp = new ExecuteTask();
                lp.execute();
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){ // TODO Auto-generated method stub
        }
        public void afterTextChanged(Editable s){
        }
    });
}

如何转换那个 setContentView ?

4

2 回答 2

6

这不应该在 onCreate 中进行,您必须覆盖 onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {             
    View rootView = inflater.inflate(R.layout.activity_googlev2, container, false);
    return rootView;
}
于 2013-05-07T16:56:44.383 回答
2

bclymer 对您的要求有正确的答案,我只想补充一点,您也可以findViewById像这样在该方法中进行处理:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {             
    View rootView = inflater.inflate(R.layout.activity_googlev2, container, false);
    textView = (AutoCompleteTextView) rootView.findViewById(R.id.autoCompleteTextView1);
    return rootView;
}

此外,onCreateView在之后调用,onCreate因此如果您尝试findViewById在您的中使用onCreate它将返回 null,因为还没有附加布局视图。

于 2013-05-07T17:06:49.230 回答