Android 未通过片段调用ListView适配器

Android 未通过片段调用ListView适配器,android,listview,android-fragments,Android,Listview,Android Fragments,当我通过MainActivity调用ListViewAdWater时,它工作正常。但如果我创建ListViewFragment,它不会被调用。我添加了ListViewFragment ListViewFragment=newListViewFragment()在MainActivity中。我有ListViewAdapter作为 public class ListViewAdapter extends ArrayAdapter<String> { public ListView

当我通过MainActivity调用ListViewAdWater时,它工作正常。但如果我创建ListViewFragment,它不会被调用。我添加了
ListViewFragment ListViewFragment=newListViewFragment()在MainActivity中。我有ListViewAdapter作为

public class ListViewAdapter extends ArrayAdapter<String> {
    public ListViewAdapter(Context context, String[] sensorArray) {
        super(context, R.layout.listview_adapter, sensorArray);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View view = layoutInflater.inflate(R.layout.listview_adapter, parent, false);
        ImageView imageView = (ImageView)  view.findViewById(R.id.imageView);
        TextView textView = (TextView) view.findViewById(R.id.textView);
        imageView.setImageResource(R.drawable.image);
        textView.setText(getItem(position));
        return view;
    }
}
当我运行调试器时,它会调用ListViewFragment,但不会进入onCreate方法中

我对xml文件做了如下更改
所以我在activity_main.xml中添加了`

<fragment  android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fragment"
    android:name="com.app.fragment.ListViewFragment"
    tools:layout="@layout/listviewfragment"/>

是这样吗?

片段需要附加到活动才能正常工作。您需要使用xml(静态)或片段管理器(动态)将片段添加到活动中

静态地,您只需将此片段放入活动的布局文件中即可添加片段:

<fragment android:name="com.example.android.fragments.MyFragment"
              android:id="@+id/my_fragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />

有关更多详细信息,请参阅和。您还可以在容器视图中使用
replace
方法,顾名思义,容器视图将替换片段。

您需要使用FragmentTransaction,并且必须在主活动布局中包含FrameLayout元素

activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"/>

您可以在将ListViewFragment添加到活动的位置附加代码吗?我在MainActivity
ListViewFragment ListViewFragment=new ListViewFragment()中添加了这一行这行不通?不行。我将添加一个答案您需要从片段的
onCreateView
而不是
R.layout.activity\u main
膨胀ListView,您应该使用
R.layout.listviewfragment
我更改为R.layout.listviewfragment,但现在我得到了java.lang.RuntimeException:无法启动活动组件信息{app/.MainActivity}:java.lang.UnsupportedOperationException:AdapterView中不支持addView(视图)查看似乎ListView不喜欢activity_main.xml中的碎片我添加了
我用xml文件编辑了问题位我得到了inflateException。是否需要为ListView添加id?请尝试
View View=inflater.inflate(R.layout.listviewfragment,container,false)
<fragment android:name="com.example.android.fragments.MyFragment"
              android:id="@+id/my_fragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
    ListViewFragment listViewFragment = new ListViewFragment();
    getFragmentManager().beginTransaction().add(R.id.container_view, listViewFragment).commit();
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"/>
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container,new ListViewFragment(), "sensorArray");
ft.commit();