tags:

views:

232

answers:

2

I am completely new to android, and pretty much a Java newb.

I have a simple app that I am building to get the hang of android's development environment - and things like click events, etc..

The app loads, and I am able to change the text in a textfield using a button handler. However, when I import the location class, and try to do a simple GPS call, the application crashes.

The problem is, everything looks good in Eclipse (error console) - and I'm not seeing any exceptions in the android emulator (DevTools). I have the logcat window open, but I haven't done anything in eclipse/code to send logcat anything (do I need to?)

Can anyone see something wrong with this? Is there a better way to troubleshoot?

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.location.*;


public class locationDisplay extends Activity {

    private EditText text;
    private Location GPSLocation;
    double dblLat;
    double dblong;
    String strLat;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // bind the layout to the activity
        text = (EditText) findViewById(R.id.EditText01);
        text.setText("No button pressed");

    }

    // Handler for each button -- Button01 is when it crashes
    public void myClickHandler(View view) {

        switch (view.getId()) {
        case R.id.Button01: 
            dblLat = GPSLocation.getLatitude();
            strLat = Double.toString(dblLat);
            text.setText(strLat);
            break;
        case R.id.Button02:
            text.setText("Button 2 was clicked");
            break;
        case R.id.Button03:
            text.setText("Button 3 was clicked");
            break;
        }
    }
+2  A: 

You GPSLocation object is null. You need to access the LocationService to get a current location. And with the emulator you will need to manually send locations.

Location Services

BrennaSoft
+2  A: 

You shouldn't need to write anything to get the default messages in LogCat; uncaught exception reports should appear automatically when your program crashes. However, sometimes LogCat and your emulator get disconnected from each other and the messages simply all disappear. Simply close Eclipse and the emulator, restart them both, and the messages should reappear. An easy way to tell whether the link has been re-established is during the boot-up of the emulator. Just as the flashing "ANDROID" text in the fancy font disappears bringing you to the lockscreen, you should see about a hundred lines of text flash by on LogCat. If that doesn't happen, then LogCat isn't getting its messages.

The way to display debugging messages in Android is to use the Log.d("some name for your log statements so you can filter the LogCat messages", "The actual debug statement here");. You'll often find people using things like a static final String LOG_TAG in their application so that they can make sure their logs always have the same tag, and hence, the filter never misses a message.

As for your actual code here, Rpond is right, you never initialised your GPSLocation object.

Steve H