无法执行 android:onClick 的方法
Cannot execute method for android:onClick
我的应用程序中有这个 activity,其中当用户单击一个按钮时,它将执行 CameraUpdate
,这是在一个方法中。这是代码:
LocationsActivity.java
public class TermLocActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap map;
public static final CameraPosition MoloTerminal =
new CameraPosition.Builder().target(new LatLng(10.699160, 122.547208))
.zoom(16)
.bearing(300)
.tilt(50)
.build();
public static final CameraPosition TagbakTerminal =
new CameraPosition.Builder().target(new LatLng(10.761854, 122.577128))
.zoom(16)
.bearing(300)
.tilt(50)
.build();
public static final CameraPosition CeresTerminal =
new CameraPosition.Builder().target(new LatLng(10.758862, 122.567774))
.zoom(16)
.bearing(300)
.tilt(50)
.build();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_term_loc);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions().position(new LatLng(10.699160, 122.547208)).title("San Pedro Molo Terminal"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.699160, 122.547208), 16));
}
public void onGoToMoloTerm(View v) {
map.animateCamera(CameraUpdateFactory.newCameraPosition(MoloTerminal));
}
public void onGoToTagbakTerm(View v) {
map.animateCamera(CameraUpdateFactory.newCameraPosition(TagbakTerminal));
}
public void onGoToCeresTerm(View v) {
map.animateCamera(CameraUpdateFactory.newCameraPosition(CeresTerminal));
}
}
这是 XML 文件:
activity_locations.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
tools:context="com.thesis.iwander.TermLocActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
class="com.google.android.gms.maps.SupportMapFragment"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Molo Term."
android:id="@+id/button"
android:onClick="onGoToMoloTerm"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ceres Term."
android:id="@+id/button2"
android:onClick="onGoToCeresTerm"
android:layout_alignTop="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="13dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tagbak Term."
android:id="@+id/button3"
android:onClick="onGoToTagbakTerm"
android:layout_alignTop="@+id/button2"
android:layout_alignRight="@+id/map"
android:layout_alignEnd="@+id/map" />
</RelativeLayout>
所以每当我测试应用程序并单击其中一个按钮时,应用程序就会崩溃并且 logcat 显示此错误:
FATAL EXCEPTION: main
Process: com.thesis.iwander, PID: 28783
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener
.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18779)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener
.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18779)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.thesis.iwander.TermLocActivity.onGoToCeresTerm(TermLocActivity.java:77)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener
.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18779)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
我试图找到与我相似的问题,但找不到。
您的 xml 中的 onClick 名称错误,Java 文件中的名称错误(它们需要匹配),或者签名错误(应该 return void并以单个View作为参数)
您的对象 GoogleMap map
未初始化,因此您收到 NullPointer 异常。
快速修复:
做map = mapFragment.getMap();
我的应用程序中有这个 activity,其中当用户单击一个按钮时,它将执行 CameraUpdate
,这是在一个方法中。这是代码:
LocationsActivity.java
public class TermLocActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap map;
public static final CameraPosition MoloTerminal =
new CameraPosition.Builder().target(new LatLng(10.699160, 122.547208))
.zoom(16)
.bearing(300)
.tilt(50)
.build();
public static final CameraPosition TagbakTerminal =
new CameraPosition.Builder().target(new LatLng(10.761854, 122.577128))
.zoom(16)
.bearing(300)
.tilt(50)
.build();
public static final CameraPosition CeresTerminal =
new CameraPosition.Builder().target(new LatLng(10.758862, 122.567774))
.zoom(16)
.bearing(300)
.tilt(50)
.build();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_term_loc);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions().position(new LatLng(10.699160, 122.547208)).title("San Pedro Molo Terminal"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.699160, 122.547208), 16));
}
public void onGoToMoloTerm(View v) {
map.animateCamera(CameraUpdateFactory.newCameraPosition(MoloTerminal));
}
public void onGoToTagbakTerm(View v) {
map.animateCamera(CameraUpdateFactory.newCameraPosition(TagbakTerminal));
}
public void onGoToCeresTerm(View v) {
map.animateCamera(CameraUpdateFactory.newCameraPosition(CeresTerminal));
}
}
这是 XML 文件:
activity_locations.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
tools:context="com.thesis.iwander.TermLocActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
class="com.google.android.gms.maps.SupportMapFragment"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Molo Term."
android:id="@+id/button"
android:onClick="onGoToMoloTerm"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ceres Term."
android:id="@+id/button2"
android:onClick="onGoToCeresTerm"
android:layout_alignTop="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="13dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tagbak Term."
android:id="@+id/button3"
android:onClick="onGoToTagbakTerm"
android:layout_alignTop="@+id/button2"
android:layout_alignRight="@+id/map"
android:layout_alignEnd="@+id/map" />
</RelativeLayout>
所以每当我测试应用程序并单击其中一个按钮时,应用程序就会崩溃并且 logcat 显示此错误:
FATAL EXCEPTION: main
Process: com.thesis.iwander, PID: 28783
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener
.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18779)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener
.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18779)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.thesis.iwander.TermLocActivity.onGoToCeresTerm(TermLocActivity.java:77)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener
.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18779)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
我试图找到与我相似的问题,但找不到。
您的 xml 中的 onClick 名称错误,Java 文件中的名称错误(它们需要匹配),或者签名错误(应该 return void并以单个View作为参数)
您的对象 GoogleMap map
未初始化,因此您收到 NullPointer 异常。
快速修复:
做map = mapFragment.getMap();