android - fragment 中的 setContentView

标签 android android-fragments android-activity

我正在尝试将 Activity 转换为 fragment 。 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 ?

最佳答案

这不应该放在onCreate中,你必须覆盖onCreateView

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

关于android - fragment 中的 setContentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16424538/

相关文章:

android - 测试互联网连接 Android

android - RecyclerView LayoutManager findViewByPosition 返回 null

java - 如何正确地从 Fragment 返回一个变量?

Android Action Bar 选项卡 - 内部 fragment 事务问题

java - 如何从非 ui 线程(从表面 View 绘制线程)启动 Activity

android:screenOrientation=”locked” 在旧操作系统版本上

java - FLAG_ACTIVITY_CLEAR_TOP 无效

java - 无法解析NotificationManagerCompat - Android Wear

android - 子 fragment 替换父 fragment 根布局

连接 Activity 的 Android 最佳实践