我的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“
任何人都可以帮我解决这个错误吗?
为什么会这样?
任何帮助都会非常感激。
谢谢。
答案 0 :(得分:2)
问题是因为
naja = (Spinner)findViewById(R.id.simpleGM_adress);
和
e = (EditText)findViewById(R.id.simpleGM_adress);
第一个是Spinner
,而不是EditText
,您无法将其强制转换为EditText。
答案 1 :(得分:1)
java.lang.ClassCastException:android.widget.xx(在你的情况下,类似于按钮或spinnar的小部件类型)是因为你在XML中定义的小部件与你在java中使用的小部件不同,所以我建议你检查你的XML文件(不是mainfest文件),或者只是在这里发布。