0

我在将ArrayListOverlayItemsCustomPinpoint课堂上创建的 链接到Listview. 基本上我试图在一个小列表中显示我在地图上添加的标记。但是使用下面的代码,我得到一个NullPointerException错误。我明白为什么(我认为这是因为我创建了 2 个不相互引用的单独的“自定义”实例),但我不明白我该如何解决它......

先感谢您。

package com.lars.pinpoint;

import java.io.IOException;

import java.util.List;
import java.util.Locale;

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.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.lars.pinpoint.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;



public class Main extends MapActivity implements OnTabChangeListener{
    /** Called when the activity is first created. */
    private static final String LIST_TAB_TAG = "List";
    private static final String MAP_TAB_TAG = "Map";

MapView map;
ListView listView;

TabHost tabHost;

long start;
long stop;
int x, y;

MyLocationOverlay compass;
MyLocationOverlay MyLoc;
MapController controller;

GeoPoint touchedPoint;
Drawable d;
List<Overlay> overlayList;
CustomPinpoint custom;



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

    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();

    listView = (ListView) findViewById(R.id.list);
    listView.setEmptyView((TextView) findViewById(R.id.empty));
    d = getResources().getDrawable(R.drawable.ic_launcher);
    CustomPinpoint custom = new CustomPinpoint(d,Main.this);
    listView.setAdapter(new ArrayAdapter<OverlayItem>(this, android.R.layout.simple_list_item_1, custom.pinpoints));
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            GeoPoint geoPoint = ((OverlayItem) listView.getAdapter().getItem(position)).getPoint();
            if(geoPoint != null) {

                map.getController().animateTo(geoPoint);

                tabHost.setCurrentTab(1);

        }

        }
    });

    map = (MapView) findViewById(R.id.mapview);
    map.setBuiltInZoomControls(true);
    map.postInvalidate();

    Touch t = new Touch();
    overlayList = map.getOverlays();
    overlayList.add(t);
    compass = new MyLocationOverlay(Main.this, map);
    overlayList.add(compass);
    controller = map.getController();



    MyLoc = new MyLocationOverlay(Main.this, map);
    overlayList.add(MyLoc);
    map.postInvalidate();
    MyLoc.runOnFirstFix(new Runnable() {
        public void run() {
            map.getController().animateTo(MyLoc.getMyLocation());
            }
    }); 

    tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG).setIndicator("List").setContent(new TabContentFactory() {
        public View createTabContent(String arg0) {
            return listView;
        }
    }));
    tabHost.addTab(tabHost.newTabSpec(MAP_TAB_TAG).setIndicator("Map").setContent(new TabContentFactory() {
        public View createTabContent(String arg0) {
            return map;
        }
    }));

    //HACK to get the list view to show up first,
    // otherwise the mapview would be bleeding through and visible
    tabHost.setCurrentTab(1);
    tabHost.setCurrentTab(0);


}



@Override
protected void onPause() {
    // TODO Auto-generated method stub
    compass.disableCompass();
    super.onPause();
    MyLoc.disableMyLocation();
    finish();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    compass.enableCompass();
    super.onResume();
    MyLoc.enableMyLocation();

}

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



class Touch extends Overlay {
    public boolean onTouchEvent(MotionEvent e, MapView m) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            start = e.getEventTime();
            x = (int) e.getX();
            y = (int) e.getY();
            touchedPoint = map.getProjection().fromPixels(x, y);

        }
        if (e.getAction() == MotionEvent.ACTION_UP) {
            stop = e.getEventTime();
        }
        if (stop - start > 1500) {
            AlertDialog alert = new AlertDialog.Builder(Main.this).create();
            alert.setTitle("Pick an option.");

            alert.setButton(DialogInterface.BUTTON_POSITIVE,"Place a pinpoint.",
                    new DialogInterface.OnClickListener() {


                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                            OverlayItem overlayItem = new OverlayItem(touchedPoint, "Pinpoint", "2nd String");
                            custom.insertPinpoint(overlayItem);
                            overlayList.add(custom);




                        }
                    });
            alert.setButton(DialogInterface.BUTTON_NEUTRAL,"Get address.",
                    new DialogInterface.OnClickListener() {


                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                        Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
                            try{

                                List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6() /1E6, touchedPoint.getLongitudeE6()/1E6, 1);                          

                                if (address.size() > 0){
                                    String display = "";                                                
                                    for (int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++){

                                        display += address.get(0).getAddressLine(i) + "\n";
                                    }
                                    Toast t3 = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
                                    t3.show();
                                }

                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }finally{

                            }

                        }
                    });
            alert.setButton(DialogInterface.BUTTON_NEGATIVE,"Toggle View", new DialogInterface.OnClickListener() {


                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                    if (map.isSatellite()){
                        map.setSatellite(false);

                    }else{

                        map.setSatellite(true);
                    }


                }
            });
            alert.show();
            return true;
        }

        return false;
    }
}




 public void gpsCurrentLocation()
 {

     tabHost.setCurrentTab(1);
     GeoPoint p = MyLoc.getMyLocation();
     map.getController().animateTo(p);

 }


// Menu XML file (menu.xml)
 @Override
 public boolean onCreateOptionsMenu(Menu menu)
 {
 MenuInflater menuInflater = getMenuInflater();
 menuInflater.inflate(R.menu.activity_main, menu);
 return true;
 }

 /**
 * Event Handling for Individual menu item selected
 * Identify single menu item by it's id
 * */

 @Override
 public boolean onOptionsItemSelected(MenuItem item)
 {

 switch (item.getItemId())
 {
 case R.id.my_location:
 Toast.makeText(Main.this, "Moving To Current location", Toast.LENGTH_SHORT).show();
 gpsCurrentLocation();

 return true;

 default:
 return super.onOptionsItemSelected(item);
 }
 }



public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub

}

}

4

1 回答 1

0

采用 custom = new CustomPinpoint(d,Main.this);

代替

  CustomPinpoint custom = new CustomPinpoint(d,Main.this);
于 2012-07-21T12:03:13.583 回答