Java NullPointerException:尝试在EditText上调用虚拟方法
2019-02-13 21:35:52.118 9765-9765/E/AndroidRuntime:致命异常:主 工艺流程:PID:9765 java.lang.RuntimeException:无法启动活动:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google.android.gms.location.places.ui.PlaceSelectionListener)” 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)上 位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789) 位于android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 位于android.os.Handler.dispatchMessage(Handler.java:110) 位于android.os.Looper.loop(Looper.java:203) 位于android.app.ActivityThread.main(ActivityThread.java:6251) 位于java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)上 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google.android.gms.location.places.ui.PlaceSelectionListener)” 在uber.DriverEdm.onCreate上(DriverEdm.java:180) 位于android.app.Activity.performCreate(Activity.java:6673) 位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)上 位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789) 位于android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 位于android.os.Handler.dispatchMessage(Handler.java:110) 位于android.os.Looper.loop(Looper.java:203) 位于android.app.ActivityThread.main(ActivityThread.java:6251) 位于java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)上 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)`Java NullPointerException:尝试在EditText上调用虚拟方法,java,android,android-studio,nullreferenceexception,Java,Android,Android Studio,Nullreferenceexception,2019-02-13 21:35:52.118 9765-9765/E/AndroidRuntime:致命异常:主 工艺流程:PID:9765 java.lang.RuntimeException:无法启动活动:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListene
正如我所见,NullPointerException位于AutoCompleteFragment中,而不是EditText中 您可以尝试此更改,请告诉我它是否适用于您: 替换
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocomplete"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="0dp"
android:layout_weight="5"
app:layout_constraintBottom_toTopOf="@+id/map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="@+id/pannel"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="8dp"
android:layout_weight="5"
app:layout_constraintBottom_toTopOf="@+id/pannel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/place_autocomplete_fragment" />
<LinearLayout
android:id="@+id/pannel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/map">
<Switch
android:id="@+id/workingSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Working" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_weight="1"
android:text="Find User" />
</LinearLayout>
</LinearLayout>
大家好,欢迎!StackOverflow是一个人们可以提出问题、展示他们尝试过的内容并获得答案(希望如此)的地方。请务必格式化代码并添加到目前为止您为解决此问题所做的工作,好吗?@One午餐人我很抱歉,请重新键入,或者它无法解决相同的问题
--------- beginning of crash
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocomplete"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="0dp"
android:layout_weight="5"
app:layout_constraintBottom_toTopOf="@+id/map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="@+id/pannel"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="8dp"
android:layout_weight="5"
app:layout_constraintBottom_toTopOf="@+id/pannel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/place_autocomplete_fragment" />
<LinearLayout
android:id="@+id/pannel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/map">
<Switch
android:id="@+id/workingSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Working" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_weight="1"
android:text="Find User" />
</LinearLayout>
</LinearLayout>
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocomplete"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="0dp"
android:layout_weight="5"
app:layout_constraintBottom_toTopOf="@+id/map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="0dp"
android:layout_weight="5"
app:layout_constraintBottom_toTopOf="@+id/map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
SupportPlaceAutocompleteFragment places = (SupportPlaceAutocompleteFragment)
getChildFragmentManager()
.findFragmentById(R.id.place_autocomplete_fragment);