0

我正在尝试一个我在互联网上获得的项目,但是当我在 Eclipse Juno 上导入它时,activity_main.xml 中的项目无法使用。这是activity_main.xml 代码`

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >      

    <Button 
        android:id="@+id/btn_find"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@string/str_btn_find" 
        android:layout_alignParentRight="true" />

    <EditText
        android:id="@+id/et_location"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:inputType="text" 
        android:hint="@string/hnt_et_location"
        android:layout_toLeftOf="@id/btn_find" />

</RelativeLayout>

 <com.google.android.maps.MapView        
    android:id="@+id/map_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:apiKey="02XkocQETlvu36mzmDnwYr-A4GTX5mENmESOG8A"
    android:clickable="true" />

`

这是 MainActivity.java 的代码

import java.io.IOException;
import java.util.List;

import android.R;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MainActivity extends MapActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting reference to btn_find of the layout activity_main
    Button btn_find = (Button) findViewById(R.id.btn_find);

    // Defining button click event listener for the find button
    OnClickListener findClickListener = new OnClickListener() {         
        @Override
        public void onClick(View v) {
            // Getting reference to EditText to get the user input location
            EditText etLocation = (EditText) findViewById(R.id.et_location);

            // Getting user input location
            String location = etLocation.getText().toString();

            if(location!=null && !location.equals("")){
                new GeocoderTask().execute(location);
            }
        }
    };

    // Setting button click event listener for the find button
    btn_find.setOnClickListener(findClickListener);     
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

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

// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

    @Override
    protected List<Address> doInBackground(String... locationName) {
        // Creating an instance of Geocoder class
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address> addresses = null;

        try {
            // Getting a maximum of 3 Address that matches the input text
            addresses = geocoder.getFromLocationName(locationName[0], 3);
        } catch (IOException e) {
            e.printStackTrace();
        }           
        return addresses;
    }


    @Override
    protected void onPostExecute(List<Address> addresses) {

        // Getting Reference to MapView of the layout activity_main
        MapView mapView = (MapView) findViewById(R.id);

        // Setting ZoomControls
        mapView.setBuiltInZoomControls(true);

        // Getting MapController for the MapView
        MapController mc = mapView.getController();


        // Getting Drawable object corresponding to a resource image
        Drawable drawable = getResources().getDrawable(R.drawable.marker);


        // Getting Overlays of the map
        List<Overlay> overlays = mapView.getOverlays();

        // Creating an ItemizedOverlay
        LocationOverlay locationOverlay = new LocationOverlay(drawable,getBaseContext());

        // Clearing the overlays
        overlays.clear();

        if(addresses==null || addresses.size()==0){
            Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();

            // Redraws the map to clear the overlays
            mapView.invalidate();
        }

        // Adding Markers on Google Map for each matching address
        for(int i=0;i<addresses.size();i++){                

            Address address = (Address) addresses.get(i);

            // Creating an instance of GeoPoint, to display in Google Map
            GeoPoint p = new GeoPoint(
                                      (int)(address.getLatitude()*1E6),
                                      (int)(address.getLongitude()*1E6)
                                     );

            String addressText = String.format("%s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getCountryName());


            // Creating an OverlayItem to mark the point
            OverlayItem overlayItem = new OverlayItem(p, "Location",addressText);

            // Adding the OverlayItem in the LocationOverlay
            locationOverlay.addOverlay(overlayItem);

            // Adding locationOverlay to the overlay
            overlays.add(locationOverlay);

            // Locate the first location
            if(i==0)
                mc.animateTo(p);                    
        }

        // Redraws the map
        mapView.invalidate();

    }       
}

}`

该错误在“R.layout.activity_main”、“Button btn_find = (Button) findViewById(R.id.btn_find)”之类的行上显示了一些错误。activity_main 和 btn_find 以及另一个 R.id.x 标记为错误。那里有什么问题?谢谢。

4

2 回答 2

0

添加这个 xmlns:android="http://schemas.android.com/apk/res/android" 之后

于 2013-01-25T06:41:41.753 回答
0

您需要做的第一件事是 OnClickListener 的类应如下所示:-

 public void onCreate(Bundle savedInstanceState){

 Button btn_find = (Button) findViewById(R.id.btn_find);
 btn_find.setOnClickListener(findClickListener);

}

 OnClickListener findClickListener = new OnClickListener() {         
    @Override
    public void onClick(View v) {
        // Getting reference to EditText to get the user input location
        EditText etLocation = (EditText) findViewById(R.id.et_location);

        // Getting user input location
        String location = etLocation.getText().toString();

        if(location!=null && !location.equals("")){
            new GeocoderTask().execute(location);
        }
    }
};

看看这个并尝试清理你的项目......!!!!

使用您的布局如下: -

<RelativeLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >      

<Button 
    android:id="@+id/btn_find"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/str_btn_find" 
    android:layout_alignParentRight="true" />

<EditText
    android:id="@+id/et_location"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"        
    android:inputType="text" 
    android:hint="@string/hnt_et_location"
    android:layout_toLeftOf="@id/btn_find" />

 <com.google.android.maps.MapView        
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:apiKey="02XkocQETlvu36mzmDnwYr-A4GTX5mENmESOG8A"
android:clickable="true" />


</RelativeLayout>

并检查出来......!!!!

于 2013-01-25T06:22:08.020 回答