我正在开发者网站上阅读Fragment上的文档。我创建了一个针对api 8的新项目:android 2.2。我安装了Android支持库,并将android支持库添加到项目中。我开始实现帮助方法showDetails,我得到了红色波浪线,说“a_item无法解析”。我不记得说创建a_item变量或添加a_item id的布局的说明。 另外,在详细活动中,我也收到了错误消息。 (getFragmentManager()。beginTransaction()。add(android.R.id.content,details).commit();)它说“FragmentTransaction类型中的方法add(int,Fragment)不适用于参数(int ,DetailsFragment)“。我在指示中遗漏了什么吗?
以下是我的示例代码:
package com.example.fragmentlayout;
import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
public class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getResources().getConfiguration().ORIENTATION_LANDSCAPE == Configuration.ORIENTATION_LANDSCAPE )
{
finish();
return;
}
if(savedInstanceState == null)
{
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
}
这是标题碎片:
package com.example.fragmentlayout;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition =0;
String[] titles = {"Title 1", "Title 2", "Title 3"};
String[] Details = {"Details for one", "Details for two", "Details for Three"};
@SuppressLint("InlinedApi")
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, titles));
View detailsFrame = getActivity().findViewById(R.id.fragment2);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if(savedInstanceState != null)
{
mCurCheckPosition = savedInstanceState.getInt("curChoise", 0);
}
if(mDualPane)
{
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition);
}
}
private void showDetails(int index) {
mCurCheckPosition = index;
if(mDualPane)
{
getListView().setItemChecked(index, true);
DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.fragment2);
if(details == null || details.getShownIndex() != index)
{
details = DetailsFragment.newInstance(index);
FragmentTransaction ft = getFragmentManager().beginTransaction();
if(index ==0)
{
ft.replace(R.id.fragment2, details);
} else
{
ft.replace(R.id.a_item, details);
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else
{
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
}
这是detailsFragment:
package com.example.fragmentlayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;
public class DetailsFragment extends Fragment {
String[] Details = {"Details for one", "Details for two", "Details for Three"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container == null)
{
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Details[getShownIndex()]);
return scroller;
// return super.onCreateView(inflater, container, savedInstanceState);
}
public static DetailsFragment newInstance(int index)
{
DetailsFragment f = new DetailsFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex()
{
return getArguments().getInt("index", 0);
}
}
这是土地和人像布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentlayout.TitlesFragment"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentlayout.DetailsFragment"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentlayout.TitlesFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
答案 0 :(得分:1)
也许,这是一个错误。答案就在这里 https://code.google.com/p/android/issues/detail?id=54837
链接消息的作者建议替换代码片段
if (index == 0) {
ft.replace(R.id.details, details);
} else {
ft.replace(R.id.a_item, details);
}
代码片段
if (index == 0) {
ft.add(R.id.details, details);
} else {
ft.replace(R.id.details, details);
}
或者只是
ft.replace(R.id.details, details);
答案 1 :(得分:0)
如果将片段嵌套到其他片段中,请考虑使用getChildFragmentManager()
(好吧,我没看完所有代码,所以不确定3是否适用)
答案 2 :(得分:-1)
您需要使用getSupportFragmentManager而不是getFragmentManager,因为您正在使用支持片段。
R.id.a_item引用布局文件中布局的ID。而不是android:id =“@ + id / fragment1”,使它成为android:id =“@ + id / a_item”