Android fragment-findviewbyd返回null

Android fragment-findviewbyd返回null,android,android-fragments,nullpointerexception,findviewbyid,Android,Android Fragments,Nullpointerexception,Findviewbyid,我有一个在互联网上很常见的问题:我用一个片段创建了一个活动。这是生成的代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_customer); if (savedInstanceState == null) {

我有一个在互联网上很常见的问题:我用一个片段创建了一个活动。这是生成的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
            ListView lv = (ListView) findViewById(android.R.id.list);
        }
    }
在该条件之后或内部,我无法获取任何具有findViewById的对象:它始终为null。以下是生成的代码的其余部分:

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_customer,
                container, false);
        return rootView;
    }
activity_customer.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.axacs.marc.CustomerActivity"
    tools:ignore="MergeRootFrame" />
<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/new_folder_button"
    android:text="" />

listview实际上位于fragment_customer.xml中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.axacs.marc.CustomerActivity"
    tools:ignore="MergeRootFrame" />
<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/new_folder_button"
    android:text="" />


有人知道缺少了什么吗?

如果要在片段中使用listview,请在将此行移动到onCreateView()后使用以下命令

ListView lv = (ListView)rootview.findViewById(android.R.id.list);

Listview属于
fragment\u customer.xml
。发布sameDo以干净地构建您的项目。显示您的活动\u customer.xmlListView lv=(ListView)findViewById(android.R.id.list);在fragment onCreateView()上移动此行,查看rootView=inflater.inflate(R.layout.fragment\u customer,container,false);ListView lv=(ListView)rootView.findviewbyd(android.R.id.list);返回rootView非常感谢。我不习惯碎片,但我的问题似乎很简单。我在使用viewpager时也遇到过同样的问题,但您的解决方案在这种情况下不起作用。如果它一直困扰着我,我会发布另一个问题。再次感谢你
无法解析“rootview”符号(@dialex rootview不是预定义的符号。它是给定给要在片段中膨胀的视图的名称。它可以有任何名称。@dialex您的函数->onCreateView应该像->Bundle savedInstanceState){View rootview=null;rootview=inflater.inflate(R.layout.fragment\u web\u View,container,false);ListView lv=(ListView)rootview.findViewById(android.R.id.list);返回rootview;