我知道有很多这样的情况,但我的问题是我做的一切都正确(尽我所知)但findviewbyid方法仍然返回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$1.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$1.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)
这是堆栈跟踪。
EDIT!
已发现问题。它没有用,因为按钮不一定在&#34; RelativeLayout&#34; rootview但在&#34; MapsView&#34;根。 因为我正在膨胀相对布局,因为某种原因它无法使按钮膨胀。
答案 0 :(得分:3)
您在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;
}
编辑:
尝试阅读您发布的堆栈跟踪。
这条线清楚地说明了一切:
com.example.sanchez.worldgramproject.Fragments.MapsFragment.onViewCreated(MapsFragment.java:224)
您的代码在“onViewCreated”方法中崩溃,而不是“onCreateView”(再次,不同的方法)。
它甚至在代码中有一个特定的行号以供参考。
尝试并发布您的FULL Fragment类代码,如果您只提供部分数据,则很难帮助您。