2

アクティビティをフラグメントに変換しようとしています。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

2

findViewByIdbclymer には、あなたが求めていることに対する正しい答えがあります。そのメソッド内で次のように処理することもできることを付け加えたいと思います。

@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、使用しようとすると null が返さfindViewByIdれます。onCreate

于 2013-05-07T17:06:49.230 に答える