2012-07-20 2 views
0

내가 만든 마커를 목록의 MapView에 표시하려고합니다. CustomPinpoint 클래스에서 Mapview에서 마커를 표시하는 데 사용하는 오버레이 항목 Arraylist를 만들고 다른 마커를 요약하기 위해 Listview에서 만듭니다. 그러나 listview를 설정하려고 할 때 정적이 아닌 필드에 대한 정적 참조를 만들 수없는 오류가 발생합니다. 내가 왜이 실수를하는지 알지만,이 문제를 해결하는 방법을 모르거나 이해하지 못합니다. (그건 그렇고, 마커를 저장하는 마커의 데이터베이스에게 가장 좋은 방법을 만들어? 또는 다른 더 좋은 방법입니다입니까?)정적이 아닌 필드 (안드로이드)에 대한 정적 참조

인사말,

Main.java에게

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.Context; 
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; 




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

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

     // setup list view 
     listView = (ListView) findViewById(R.id.list); 
     listView.setEmptyView((TextView) findViewById(R.id.empty)); 

     // create some dummy coordinates to add to the list 

     listView.setAdapter(new ArrayAdapter<OverlayItem>(this, android.R.layout.simple_list_item_1, CustomPinpoint.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(); 

     d = getResources().getDrawable(R.drawable.ic_launcher); 

     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"); 
           CustomPinpoint custom = new CustomPinpoint(d, Main.this); 
           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() 
    { 

     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 

    } 


} 

CustomPinpoint.java을

package com.lars.pinpoint; 

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 

import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.OverlayItem; 

public class CustomPinpoint extends ItemizedOverlay<OverlayItem>{ 

public ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>(); 


public CustomPinpoint(Drawable defaultMarker) { 
    super(boundCenter(defaultMarker)); 
    // TODO Auto-generated constructor stub 
} 

public CustomPinpoint(Drawable m, Context context) { 
    // TODO Auto-generated constructor stub 
    this(m); 
    Context c = context; 
} 

@Override 
protected OverlayItem createItem(int i) { 
    // TODO Auto-generated method stub 
    return pinpoints.get(i); 
} 

@Override 
public int size() { 
    // TODO Auto-generated method stub 
    return pinpoints.size(); 
} 

public void insertPinpoint(OverlayItem item){ 
    pinpoints.add(item); 
    this.populate(); 
} 

}

PS. 어리석은 질문에 대해 유감 스럽지만 이것은 모두 나를 위해 비교적 새로운 것이다

답변

0

비 정적 필드는 클래스의 각 인스턴스에 속하므로 클래스에서 직접 액세스 할 수 없습니다. 클래스 실체화

  • 하고 결과 개체에서 필드에 액세스 :

    그래서 당신도 할 필요가있다. 이 경우 싱글 톤을 만드는 것이 유용 할 수 있습니다.

  • 코드의 호출 부분을 비 정적으로 선언하십시오.
+0

나는 정말로 그것을 얻지 못한다. 정적 인 목록을 선언하면 작동하지 않습니다 ... – user1534543

+0

이상한 것은 그것이 1 개의 마커에서 작동하지만 여러 마커에서는 작동하지 않는다는 것입니다. 그때 그냥 충돌이 – user1534543

+0

여보세요? 아무도 도와 줄 수 있니?. – user1534543

관련 문제