开发者

Android: who can help me with setting up this google maps class please?

Firstly this has turned out to be quite a long post so please bear with me as its not too difficult but you may need to clarify something with me if i haven't explained it correctly. So with some help the other day from guys on this forum, i managed to partially set up my 'mapClass' class, but i'm having trouble with it and its not running correctly so i would like some help if possible. I will post the code below so you can see.

What Ive got is a 'Dundrum' class which sets up the listView for an array of items.

Then ive got a 'dundrumSelector' class which I use to set up the setOnClickListener() methods on the listItems and link them to their correct views.

THIS IS MY DUNDDRUM SELECTOR CLASS....

    public static final int BUTTON1 = R.id.anandaAddressButton;
public static final int BUTTON2 = R.id.bramblesCafeAddressButton;
public static final int BUTTON3 = R.id.brannigansAddressButton;

public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);

    int position = getIntent().getExtras().getInt("position");

    if(position == 0){
        setContentView(R.layout.ananda);
    };
    if(position == 1){
        setContentView(R.layout.bramblescafe);
    };
    if(position == 2){
        setContentView(R.layout.brannigans);
Button anandabutton = (Button) findViewById(R.id.anandaAddressButton);
anandabutton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(),MapClass.class);
        myIntent.putExtra("button", BUTTON1);
        startActivityForResult(myIntent,0);

    }

});

Button bramblesbutton = (Button) findViewById(R.id.bramblesCafeAddressButton);
bramblesbutton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(),MapClass.class);
        myIntent.putExtra("button", BUTTON2);
        startActivityForResult(myIntent, 0);
    }

});

etc etc....

Then what i did was set up static ints to represent the buttons which you can see at the top of this class, the reason for this is because in my mapClass activity I just want to have one method, because the only thing that is varying is the coordinates to each location. ie. i dont want to have 100+ map classes essentially doing the same thing other than different coordinates into the method.

So my map class is as follows...

case DundrumSelector.BUTTON1:
        handleCoordinates("53.288719","-6.241179");
        break;
    case DundrumSelector.BUTTON2:
        handleCoordinates("53.288719","-6.241179");
        break;
    case DundrumSelector.BUTTON3:
        handleCoordinates("53.288719","-6.241179");
        break;
    }
}




private void handleCoordinates(String l, String b){


    mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
    View zoomView = mapView.getZoo开发者_StackOverflow社区mControls();

    zoomLayout.addView(zoomView,
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

    mc = mapView.getController();
    String coordinates[] = {l, b};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
            (int) (lat*1E6),
            (int) (lng*1E6));

    mc.animateTo(p);
    mc.setZoom(17);
    mapView.invalidate();


}

Now this is where my problem is. The onClick() events don't even work from the listView to get into the correct views. I have to comment out the methods in 'DundrumSelector' before I can get into their views.

And this is what I dont understand, firstly why wont the onClick() events work, because its not even on that next view where the map is.

I know this is a very long post and it might be quite confusing so let me know if you want any clarification..

Just to recap, what i'm trying to do is just have one class that sets up the map coordinates, like what i'm trying to do in my 'mapClass'.

Please can someone help or suggest another way of doing this! Thanks alot everyone for reading this.


The Problem ist the following:

Button bramblesbutton = (Button) findViewById(R.id.bramblesCafeAddressButton);
bramblesbutton.setOnClickListener(new View.OnClickListener() {

Here you try to set the onClickListener to a button which is only available if you're at position 2 and have called the correct setContentView() method. If you have another View as content (which does not contain your button) then

Button bramblesbutton = (Button) findViewById(R.id.bramblesCafeAddressButton);

will return null and in the next line there is a NullPointerException. So you have to only add this onClickListener if you're also using the corresponding layout.

And yes it works if you comment it out, because then you dont try to call a method on null. But on the other hand, if you comment it out, then it wont register your clicks and therefore not proceed.

To make it even clearer:

when your passing position 0, then you're setting this ContentView:

if(position == 0){
        setContentView(R.layout.ananda);
    };

Therefore I assume the other two Buttons will not be in this layout anada and therefore assigning a ClickListener to them will throw a NullPointerException.

To solve this issue and if you really need this 3 different layouts, I would do it that way (example for the first button):

if(position == 0){
        setContentView(R.layout.ananda);
        Button anandabutton = (Button) findViewById(R.id.anandaAddressButton);
        anandabutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               Intent myIntent = new Intent(view.getContext(),MapClass.class);
               myIntent.putExtra("button", BUTTON1);
               startActivityForResult(myIntent,0);
            }
        });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜