-1

我的 android 代码有问题

这是我的 MainActivity.java

public class MainActivity extends MapActivity {

        MapView mapView; 
        MapController mc;
        GeoPoint p;
        Button b;
        EditText e;
        TextView t;
        String x;
        String longi ;
        String lati ;
        Spinner naja;

        String[] jalan = {"A.Yani","Ambengan", "Anjasmoro","Baliwerti","Basuki Rahmat","Bintoro","Ciliwung","Darmo Raya","Darmokali","Diponegoro Raya","Dharmawangsa","Dinoyo","Embong Kemiri","Embong Malang"};
        class MapOverlay extends com.google.android.maps.Overlay
        {
            @Override
            public boolean draw(Canvas canvas, MapView mapView, 
            boolean shadow, long when) 
            {
                super.draw(canvas, mapView, shadow);                   

                //---translate the GeoPoint to screen pixels---
                Point screenPts = new Point();
                mapView.getProjection().toPixels(p, screenPts);

                //---add the marker---
                Bitmap bmp = BitmapFactory.decodeResource(
                    getResources(), R.drawable.bawah);            
                canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
                return true;
            }
        } 
        public boolean onKeyDown(int keyCode, KeyEvent event) 
        {
            MapController mc = mapView.getController(); 
            switch (keyCode) 
            {
                case KeyEvent.KEYCODE_3:
                    mc.zoomIn();
                    break;
                case KeyEvent.KEYCODE_1:
                    mc.zoomOut();
                    break;
            }
            return super.onKeyDown(keyCode, event);
        }    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            mapView = (MapView) findViewById(R.id.mapview);
            LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
            View zoomView = mapView.getZoomControls(); 

            naja=(Spinner)findViewById(R.id.simpleGM_adress);
            ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, jalan);
            aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
             naja.setAdapter(aa);
            t=(TextView)findViewById(R.id.simpleGM_tv);
            e=(EditText)findViewById(R.id.simpleGM_adress);
            zoomLayout.addView(zoomView, 
                new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT)); 
            mapView.displayZoomControls(true);
            /*
            mc = mapView.getController();
            String coordinates[] = {longi, lati};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);

            p = new GeoPoint(
                (int) (lat * 1E6), 
                (int) (lng * 1E6));

            mc.animateTo(p);
            mc.setZoom(17); 
            mapView.invalidate();
            mc.animateTo(p);
            mc.setZoom(17); 

            //---Add a location marker---
            MapOverlay mapOverlay = new MapOverlay();
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);        

            mapView.invalidate();*/
        }


        public void clickHandler(View view){
            switch (view.getId()){
            case R.id.simpleGM_btn_search:
                x = jalan[naja.getSelectedItemPosition()];
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("nama_jalan", x));
                /*            String valid = "1";*/ 
             String response = null;

             try {

                response = CustomHttpClient.executeHttpPost("http://192.168.1.4/android/kordinat.php", postParameters);


                String result = response.toString();  
                //parse json data

                   try{
                           longi = "";
                           lati = "";

                     JSONArray jArray = new JSONArray(result);
                           for(int i=0;i<jArray.length();i++){

                                   JSONObject json_data = jArray.getJSONObject(i);
                                   Log.i("log_tag","nama_jalan: "+json_data.getString("nama_jalan")+
                                           ", latitude: "+json_data.getString("latitude")+
                                           ", longitude: "+json_data.getString("longitude")
                                   );
                                   if(result.equals(null)){
                                   //Get an output to the screen
                                  longi += "Data tidak ada";
                                   }
                                   else{
                                   lati += json_data.getString("latitude");
                                   longi += json_data.getString("longitude");
                                   }
                           }
                   }


                   catch(JSONException e){
                           Log.e("log_tag", "Error parsing data "+e.toString());
                   }

                   try{
                       mc = mapView.getController();
                    String coordinates[] = {longi, lati};
                    double lat = Double.parseDouble(coordinates[0]);
                    double lng = Double.parseDouble(coordinates[1]);

                    p = new GeoPoint(
                        (int) (lat * 1E6), 
                        (int) (lng * 1E6));

                    mc.animateTo(p);
                    mc.setZoom(17); 
                    mapView.invalidate();
                    mc.animateTo(p);
                    mc.setZoom(17); 

                    //---Add a location marker---
                    MapOverlay mapOverlay = new MapOverlay();
                    List<Overlay> listOfOverlays = mapView.getOverlays();
                    listOfOverlays.clear();
                    listOfOverlays.add(mapOverlay);        

                    mapView.invalidate();
                   }


                   catch(Exception e){

                    Log.e("log_tag","Error in Display!" + e.toString());;          

                   }   

              }

                    catch (Exception e) {

               Log.e("log_tag","Error in http connection!!" + e.toString());     

              }
             break;
            }
        }

        @Override
        protected boolean isRouteDisplayed() {
            // TODO Auto-generated method stub
            return false;
        }
    }

这是我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/simpleGM_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
    <TextView
android:id="@+id/simpleGM_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tentukan Jalanmu"
android:layout_x="11px"
android:layout_y="5px"
>
</TextView>
<Spinner
                android:id="@+id/simpleGM_adress" 
                android:layout_width="240px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="12px"
android:layout_y="30px"
></Spinner>
<Button
android:id="@+id/simpleGM_btn_search"
android:layout_width="59px"
android:layout_height="wrap_content"
android:layout_x="256px"
android:layout_y="30px"
android:text="find"
android:onClick="clickHandler"
>
</Button>

<com.google.android.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_x="2px"
android:layout_y="100px"
android:clickable="true"
        android:apiKey="00j2bA4ivSvH0cSDJJ4aiPiVCq_OSH0adwf6I2w"
    />
    <LinearLayout android:id="@+id/zoom" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true" 
        /> 

</AbsoluteLayout>

这是我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="udin.googlemaps"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>
    <uses-permission android:name="android.permission.INTERNET" />   
</manifest>

当我运行我的程序时,它显示这样的错误

“java.lang.RuntimeException:无法启动活动
ComponentInfo{udin.googlemaps/udin.googlemaps.MainActivity}:java.lang.ClassCastException:android.widget.Spinner”

谁能帮我解决这个错误?

为什么会这样?

任何帮助将不胜感激。

谢谢你。

4

2 回答 2

2

问题是因为

   naja = (Spinner)findViewById(R.id.simpleGM_adress);

   e = (EditText)findViewById(R.id.simpleGM_adress);

第一个是 a Spinner,而不是EditText,您不能将其转换为 EditText。

于 2012-12-08T13:25:56.977 回答
1

java.lang.ClassCastException: android.widget.xx(在你的例子中是按钮或 spinnar 等小部件类型)是因为您的 XML 中定义的小部件与您在 java 中使用的小部件不同,所以我建议您检查您的 XML 文件(不是 mainfest 文件),或者只是在此处发布。

于 2012-12-08T13:27:33.670 回答