0

I'm new at Eclipse and the Android applications making so here comes a very rookie question. How can I make this function work properly? I have just copy > paste it to my public class nowActivity extends Activity { and fixed the errors that accord. The function is as follows:

package weather.right;

import weather.right.now.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class nowActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
             Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
        }else{
            showGPSDisabledAlertToUser();
        }
    }

    public void goToSo(View view) {
        goToUrl("http://erik-edgren.nu/weather");
    }

    private void goToUrl(String url) {
        Uri uriUrl = Uri.parse(url);
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
        startActivity(launchBrowser);
    }

        private void showGPSDisabledAlertToUser(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
             .setCancelable(false)
             .setPositiveButton("Goto Settings Page To Enable GPS",
                  new DialogInterface.OnClickListener(){
                  public void onClick(DialogInterface dialog, int id){
                          Intent callGPSSettingIntent = new Intent(
                                                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(callGPSSettingIntent);
                  }
             });
             alertDialogBuilder.setNegativeButton("Cancel",
                  new DialogInterface.OnClickListener(){
                  public void onClick(DialogInterface dialog, int id){
                       dialog.cancel();
                  }
             });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
        }
}

Thanks in advance.

1 Answer 1

1

protected void onCreate1(Bundle savedInstanceState) should be protected void onCreate(Bundle savedInstanceState)?

You are supposed to override the onCreate() method. See this for more details.

For Android, sub-classes of Activity are supposed to implement certain methods so to do this you have to override certain methods by matching the parent class' methods exactly. onCreate() is one such method.

For the emulator, GPS can be tested by following the guide here. Otherwise it will show up as disabled.

Sign up to request clarification or add additional context in comments.

9 Comments

Um. Yes? That's the second line in that code. What's your point? I'm new within these "walls", remember.
@ErikEdgren It should be onCreate(), not onCreate1() (note the trailing 1). However, if you're actually aiming to declare a method with the trailing 1, then disregard the response above. onCreate() would however be called automatically as it's a vital method for all Activies in Android. :)
Thanks but nothing happens once again :( I'm testing my application trough Android Virtual Device in Eclipse. That maybe is the problem? If not, how can I fix this? :)
@ninetwozero I got the "duplicated" error message when I tried to use onCreate(). I removed onCreate1() and moved it's content to onCreate(). Same thing there - nothing happens.
Change protected to public for onCreate() and see if that changes anything? Also, post the entire class.
|