2013-05-07 55 views
2

我试图将一个活动转换为片段。 setContentView上的错误标记。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转换?

回答

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; 
} 
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; 
} 

此外,onCreateViewonCreate打完如果您尝试使用findViewByIdonCreate它会返回null,因为没有布局视图武官称为d还没有。