rivaille-sama rivaille-sama - 4 months ago 12
Android Question

Cannot execute method for android:onClick

I have this activity in my application wherein when a user clicks a button, it will execute

CameraUpdate
which is inside a method. Here's the code:

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));

}
}


Here's the XML file:

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>


So whenever I test the app and click on one of the buttons, the app crashes and the logcat shows this error:

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) 


I tried to find questions which are similar to mine but I couldn't find any.

Answer

Your object GoogleMap map is not initialized, therefore you are getting a NullPointer Exception.

Quick Fix:

do map = mapFragment.getMap();