静态引用非静态字段(android)

时间:2012-07-20 13:56:28

标签: android listview static android-mapview non-static

我正在尝试在列表中显示我在MapView中创建的标记。 因此,在CustomPinpoint类中,我创建了一个覆盖项的Arraylist,我在Mapview中使用它来显示标记,并在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。我很抱歉这个愚蠢的问题,但这对我来说都是比较新的

1 个答案:

答案 0 :(得分:0)

非静态字段属于该类的每个实例,您无法直接从该类访问它。

所以你需要:

  • 实现您的课程并从结果中访问该字段 宾语。在这种情况下,制作一个单身人士可能很有用。
  • 或者将代码的调用部分声明为非静态。
相关问题