findviewbyid returns 片段中为空,即使视图已正确膨胀

findviewbyid returns null in fragment even though view inflated properly

我知道有很多这样的情况,但我的问题是我做的一切都是正确的(据我所知)但是 findviewbyid 方法仍然 returns null

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

这是堆栈跟踪

rootView = (RelativeLayout) inflater.inflate(R.layout.activity_maps, container, false);

if (container == null) {
    return null;
}


mFloatingActionButton = (FloatingActionButton) rootView.findViewById(R.id.fab);
Logoutbutton = (Button) rootView.findViewById(R.id.button);


Logoutbutton.setOnClickListener(this);
mFloatingActionButton.setOnClickListener(this);


return rootView;}

.setonclicklistener 抛出错误,因为按钮为空

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tools:context=".Fragments.MapsFragment">

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:clickable="true"
        android:src="@drawable/ic_camera_alt_black_24dp"
        android:layout_gravity="end|bottom"
      />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log Out"
        android:id="@+id/button"
        android:layout_marginTop="17dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="17dp" />

</com.google.android.gms.maps.MapView>
</RelativeLayout>

这些是按钮

E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                             at com.example.sanchez.worldgramproject.Fragments.MapsFragment.onViewCreated(MapsFragment.java:224)
                                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1315)
                                                                                             at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                                                                                             at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                                                                                             at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                                                                                             at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                                                                                             at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:709)
                                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                             at android.os.Looper.loop(Looper.java:158)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
01-31 21:33:00.746 14460-14939/com.example.sanchez.worldgramproject I/qtaguid: Tagging socket 91 with tag 3000110100000000{805310721,0} uid -1, pid: 14460, getuid(): 10248
01-31 21:33:00.876 14460-14939/com.example.sanchez.worldgramproject I/qtaguid: Untagging socket 77
01-31 21:33:00.966 14460-14460/com.example.sanchez.worldgramproject E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.sanchez.worldgramproject, PID: 14460
                                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                          at com.example.sanchez.worldgramproject.Fragments.MapsFragment.onViewCreated(MapsFragment.java:224)
                                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1315)
                                                                                          at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                                                                                          at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                                                                                          at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                                                                                          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                                                                                          at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:709)
                                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是堆栈跟踪。

编辑!

问题已找到。它没有用,因为按钮不一定在 "RelativeLayout" rootview 下,而是在 "MapsView" root 下。 由于我正在膨胀相对布局,由于某种原因它无法膨胀按钮。

您在 onCreateView 中调用 super.onCreate。这些是截然不同的方法,绝对不正确。 (不过你不应该调用 super.onCreateView)

为什么容器不能为空?有什么具体原因吗?我以前从未见过这种逻辑,也不知道它有什么帮助(很可能什么都没有)。

此代码有效:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = (RelativeLayout) inflater.inflate(R.layout.activity_maps, container, false);

    mFloatingActionButton = (FloatingActionButton) rootView.findViewById(R.id.fab);
    Logoutbutton = (Button) rootView.findViewById(R.id.button);

    Logoutbutton.setOnClickListener(this);
    mFloatingActionButton.setOnClickListener(this);

    return rootView;
}

编辑:

尝试阅读您post编辑的堆栈跟踪。

这一行清楚地说明了一切:

com.example.sanchez.worldgramproject.Fragments.MapsFragment.onViewCreated(MapsFragment.java:224)

您的代码在 "onViewCreated" 方法中崩溃,而不是 "onCreateView"(同样,不同的方法)。
它甚至在代码中有一个特定的行号以供参考。

尝试 post 你的完整片段 class 代码,如果你只提供部分数据,很难帮助你。