将活动转换为片段

时间:2015-04-15 12:46:20

标签: android android-fragments android-activity converter

我是新手在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中出现错误。

非常感谢你。

2 个答案:

答案 0 :(得分:2)

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

但是既然你真的不清楚你的问题是什么,那么它也可能是另外的东西。请使用错误消息发布堆栈跟踪。

答案 1 :(得分:0)

我最近做了同样的事情。

首先,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);
}