views:

71

answers:

1

Hi, 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.getZoomControls();

    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.

A: 

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);
            }
        });
}
Roflcoptr
the problem is what?
Capsud
sry. had some problems with mixing up comment and answer and editing.. sry dont know what happened ;)
Roflcoptr
ah ok ok i see.Right so what wud ur expert opinion be for me to get past this?thanks for clarifying the error :)
Capsud
edited my answer
Roflcoptr
ok cool yeah i really need this to be working. so that method that you've done there would be in my dundrumSelector, and you seem to just be putting the content view and onClickListener in the one? where above i had the 2 seperate yeah?and one last thing u say i need 3 different layouts? i dont see above where u have 3 layouts in ur example?thanks so far sebi
Capsud
your using three differnt layouts by calling setContenView() three times. and yes just put this whole onClickListener stuff in the corressponding if body.btw... Just a kind advice from my side: It seems that you're a very beginner in Android (and Java?) so therefore it my be better if you first try to follow simple tutorials etc. to get used to this stuff
Roflcoptr
ok thanks dude. yeah i am a bit of a beginner to android mostly but i need to do this stuff for a project ya see so i'm kinda stressin about it ha! thanks so much i'll let you know how i get on!
Capsud
thanks a million man got it working, you've helped me out alot! i'd buy u a pint if i cud :)
Capsud