处理切换按钮时遇到问题

时间:2013-07-16 22:25:18

标签: android

在我的程序中,我希望从自定义对话框中打开/关闭地图数据图层。目前,当我单击对话框中的按钮时,它会导致程序崩溃,错误是:

java.lang.IllegalStateException: Could not find a method onWaterToggled(View) in the    activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ToggleButton with id 'waterToggle'

这是我的代码

package com.fuscoe.lidoPenn;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;




public class LidoPennActivity extends Activity {
    MapView mMapView = null;
    ArcGISTiledMapServiceLayer tileLayer;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.main);

        ArcGISDynamicMapServiceLayer water = new ArcGISDynamicMapServiceLayer("*mapserverURL*");

        // Retrieve the map and initial extent from XML layout
        mMapView = (MapView)findViewById(R.id.map);
        /* create a @ArcGISTiledMapServiceLayer */

        tileLayer = new ArcGISTiledMapServiceLayer(
                "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
        // Add tiled layer to MapView
        mMapView.addLayer(tileLayer);       
        mMapView.addLayer(water);   

        //create handle on button for selecting visible layers
        ImageButton layerDialogButton = (ImageButton) findViewById(R.id.layerdialogbutton);

        layerDialogButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                //created new dialog container for this activity
                Dialog d = new Dialog(LidoPennActivity.this);

                //set content of the dialog viewer via setContentView
                d.setContentView(R.layout.dialog);  
                d.setTitle("Select Layer to View");
                d.setCanceledOnTouchOutside(true);
                d.show(); // use dialog.dismiss(); to close the dialog after layer selection is made

            }
        });             
    }   


    public void onWaterToggled(View view){

        //is the toggle on?
        boolean on = ((ToggleButton) view).isChecked();

        if (on){
            //enable layer to draw
            //but first lets toast to see if its working
            Toast toast = Toast.makeText(LidoPennActivity.this, "water has been toggled on", 5000);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();

        } else{
            //remove layer from map
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.pause();
 }

    @Override
    protected void onResume() {
        super.onResume(); 
        mMapView.unpause();
    }   

}

这是main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".LidoPennActivity"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#bfd9f1" >

        <ImageButton
            android:id="@+id/layerdialogbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:background="@null"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:src="@drawable/layersbutton" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp" >
    </LinearLayout>

<com.esri.android.map.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        initExtent="-13127763.414490955, 3976485.9111357844, -13127104.145121979, 3977403.1554752304"       
        >
    </com.esri.android.map.MapView>
</LinearLayout>

和dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/waterToggle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_horizontal"
        android:text="Water"
        android:textOff="Water"
        android:textOn="Water"
        android:typeface="serif"
        android:onClick="onWaterToggled" />

    <ToggleButton
        android:id="@+id/gasToggle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Gas"
        android:textOff="Gas"
        android:textOn="Gas"
        android:typeface="serif" />

</LinearLayout>

我在这里问了一个类似的问题:java.lang.illegalstateexception could not find a method (view) in the activity class android fragment

但是我无法获得使用我的代码提供的解决方案。我也尝试在waterToggle按钮上添加一个点击监听器,但应用程序也崩溃了。

我很抱歉发布了之前提出的问题,但那里提供的解决方案对我没有帮助。

谢谢

0 个答案:

没有答案