gpt4 book ai didi

android - 将 Activity 转换为 fragment

转载 作者:行者123 更新时间:2023-11-29 00:11:52 25 4
gpt4 key购买 nike

我刚开始在 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;
}
}

这是 fragment 中的代码:

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中出来。

非常感谢。

最佳答案

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

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

关于android - 将 Activity 转换为 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29650615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com