无法执行android的方法:onClick

时间:2016-03-06 15:51:32

标签: android invocationtargetexception

我在我的应用程序中有这个活动,当用户点击按钮时,它将执行方法内的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) 

我试图找到与我类似但我找不到的问题。

2 个答案:

答案 0 :(得分:5)

您的对象GoogleMap map未初始化,因此您获得 NullPointer异常

快速修复:

map = mapFragment.getMap();

答案 1 :(得分:1)

您的xml中的onClick中有错误的名称,Java文件中的名称错误(需要匹配)或错误的签名(它应返回void并将单个View作为参数)