将 Activity 转换为片段

Convert Activity to Fragment

我是 Android 中使用 Fragment 的新手。我在 Activity 中完成了 viewFilpper,现在我试图将此函数添加为 Fragment(我只能将其转换为 Fragment 而不是 FragmentActivity,因为整个程序都需要)。但总是报错。有没有人可以帮我解决这个问题。 这是 Activity 中的代码:

/*
* Copyright 2012 Harri Smatt Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
* or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
public class BinderActivity extends Activity implements BinderAdapter
{

private static final int[] LAYOUT_IDS = { R.layout.layout1, R.layout.layout2, R.layout.layout3, R.layout.layout4 };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        BinderView binder = (BinderView) findViewById(R.id.binder);
        binder.setAdapter(this);
    }

    @Override
    public View createView(ViewGroup container, int position)
    {
        return getLayoutInflater().inflate(LAYOUT_IDS[position], null);
    }

    @Override
    public int getCount()
    {
        return LAYOUT_IDS.length;
    }
}

这是片段中的代码:

public class BoltFragment extends Fragment implements BinderAdapter{
    private static final int[] LAYOUT_IDS = { R.layout.layout1, R.layout.layout2, R.layout.layout3, R.layout.layout4 };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        container.removeAllViews();
        View boltLayout = inflater.inflate(R.layout.fragment_bolt,
                container, false);
        // Set title bar
        ((MenuTabsActivity) getActivity())
                .setActionBarTitle("Bolt");

        BinderView binder = (BinderView) getView().findViewById(R.id.binder);
        binder.setAdapter(this);

        return boltLayout;
    }

    @Override
    public View createView(ViewGroup container, int position)
    {
        //CHANGE HERE LATER
        return getActivity().getLayoutInflater().inflate(LAYOUT_IDS[position], null);
    }

    @Override
    public int getCount()
    {
        return LAYOUT_IDS.length;
    }
}

BinderAdapter 中的代码如下:

public interface BinderAdapter
{

public View createView(ViewGroup container, int position);

public int getCount();

}

我该如何处理

return getLayoutInflater().inflate(LAYOUT_IDS[position], null);

在片段中?

因为转换还没有完成,所以Fragment肯定会出错

非常感谢。

我最近也做了同样的事情。

首先Fragment支持onCreateView函数
参考:http://developer.android.com/guide/components/fragments.html 所以将 createView() 更改为类似这样的内容。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
   return inflater.inflate(LAYOUT_IDS[position], container, false);
}

由于您没有 post 编辑错误日志,我猜测问题出在 BinderView binder = (BinderView) getView().findViewById(R.id.binder); binder.setAdapter(this); 如果您仍在创建它,则不能使用 getView(),因此它应该是 BinderView binder = (BinderView)boltLayout.findViewById(R.id.binder); binder.setAdapter(this);

但由于您真的不清楚自己的问题是什么,所以也可能是其他问题。请 post 您的堆栈跟踪和错误消息。