ThatFellaNick
ThatFellaNick

Reputation: 29

Android Studio: Popup window layout with buttons onClick

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>

.

Upvotes: 0

Views: 4432

Answers (3)

ThatFellaNick
ThatFellaNick

Reputation: 29

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

Upvotes: 1

Viral Patel
Viral Patel

Reputation: 33408

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

Upvotes: 0

chad
chad

Reputation: 141

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

Upvotes: 0

Related Questions