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

最初に行う必要があるのは、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 に答える
0

この xmlns:android="http://schemas.android.com/apk/res/android" を後に追加します

于 2013-01-25T06:41:41.753 に答える