0

Hello I am trying to make a pop up window with 4 buttons that change the map type on a Google Maps activity. I have set the on click listener in the class with the GoogleMap object, but I get this error when clicking one of the buttons.

 Process: com.student.nick.earthquakereport, PID: 7770
          java.lang.IllegalStateException: Could not find method setMapNormal(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'mapNormal'
                    at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4485)
                    at android.view.View$DeclaredOnClickListener.onClick(View.java:4449)
                    at android.view.View.performClick(View.java:5204)
                    at android.view.View$PerformClick.run(View.java:21156)
                    at android.os.Handler.handleCallback(Handler.java:739)
                    at android.os.Handler.dispatchMessage(Handler.java:95)
                    at android.os.Looper.loop(Looper.java:148)
                    at android.app.ActivityThread.main(ActivityThread.java:5466)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

The dialog is launched in the same class the onClick methods are in.

layFlator = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    ViewGroup containter = (ViewGroup) layFlator.inflate(R.layout.map_settings, null);

    mapSettings = new PopupWindow(containter, 750, 680, true);
    mapSettings.setAnimationStyle(R.style.PopupAnimation);
    mapSettings.showAtLocation(layout, Gravity.CENTER, 0,0);

    containter.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mapSettings.dismiss();
            return false;
        }
    });

The methods

public void setMapNormal(View view) {
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mapSettings.dismiss();
}

public void setMapHybrid(View view) {
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    mapSettings.dismiss();
}

public void setMapTerrain(View view) {
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    mapSettings.dismiss();
}

public void setMapSat(View view) {
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    mapSettings.dismiss();
}

Thanks for taking a look

EDIT: The layout the popup opens

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="horizontal|vertical"
android:fadingEdgeLength="4dp"
android:elevation="20dp"
android:background="@drawable/popwindow_border"
>


<Button
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:text="Normal"
    android:id="@+id/mapNormal"
    android:layout_gravity="center_horizontal"
    android:onClick="setMapNormal"/>

<Button
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:text="Hybrid"
    android:id="@+id/mapHybrid"
    android:layout_gravity="center_horizontal"
    android:onClick="setMapHybrid"/>

<Button
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:text="Terrain"
    android:id="@+id/mapTerrain"
    android:layout_gravity="center_horizontal"
    android:onClick="setMapTerrain"/>

<Button
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:text="Satellite"
    android:id="@+id/button2"
    android:layout_gravity="center_horizontal"
    android:onClick="setMapSat"/>
</LinearLayout>

.

3 Answers 3

1

So I looked around and found a tutorial, this is the proper way to do it.

layFlator = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);

    View popUp =  layFlator.inflate(R.layout.map_settings, null);

    popupWindow = new PopupWindow(popUp, 750, 680, true);
    popupWindow.setAnimationStyle(R.style.PopupAnimation);

    Button btnHybrid = (Button) popUp.findViewById(R.id.mapHybrid);
    btnHybrid.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            Log.d("hybrid", "called");
            popupWindow.dismiss();
        }
    });
1
  • 1
    Instead of putting SOLVED in the title, you can accept your own answer.
    – Bart Kiers
    Jan 14, 2016 at 22:12
0

You are not set onClickListener correctly. Take a look at android:onClick.

For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).

1
  • I have done that, its in the activity's java class the popup window is over Jan 14, 2016 at 1:28
0

Your error msg clearly says:

java.lang.IllegalStateException: Could not find method setMapNormal(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'mapNormal'

So, you are not accessing the method right in the onClickListener. You can't just call the method using

this.methodName()

inside a handler. You might want to use something like

ClassName.this.methodName()

or

getActivity().methodName()
6
  • My on click listener is set in the XML, so I cannot do it that way I guess <Button android:layout_width="180dp" android:layout_height="wrap_content" android:text="Normal" android:id="@+id/mapNormal" android:layout_gravity="center_horizontal" android:onClick="setMapNormal"/> Jan 14, 2016 at 0:54
  • The popup is an activity.xml, can i attach a class to it and just pass extras? Jan 14, 2016 at 0:55
  • I'm not sure if you can add an onClickListener that way. At least I've never happened to do it. Try removing it from there and putting it in your Java code and check of or works. Jan 14, 2016 at 0:57
  • First you need to initialize your button. It is null when you are seeing the onClickListener. Use findViewById to get the button first Jan 14, 2016 at 1:15
  • with the code showed in pastebin.com/3LdE6wp2, it seems the ''mapHybrid Button'' is in the xml which you use for the contentView of popupwindow, no the Activity. Before you use findViewbyId method in a activity, you should call setContentView(View view) method, otherwise findViewbyId will return null. Like this: setContentView(R.layout.xxx); Button button = (Button) findViewById(R.id.yyy) the button yyy is in xxx.xml
    – wuyi
    Jan 14, 2016 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.