2012-04-02 3 views
0

저는 이클립스에서 새로 왔고 안드로이드 애플리케이션이 여기에서 매우 신인상이되었습니다. 이 기능을 제대로 작동 시키려면 어떻게합니까? 나는 복사하여 내 public class nowActivity extends Activity {에 붙이고 오류를 수정했습니다. 기능은 다음과 같습니다 :GPS 기능을 작동시키는 방법은 무엇입니까?

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(); 
     } 
} 

미리 감사드립니다.

답변

1

protected void onCreate1(Bundle savedInstanceState)protected void onCreate(Bundle savedInstanceState)이어야합니다.

onCreate() 메서드를 재정의해야합니다. 자세한 내용은 this을 참조하십시오.

Android의 경우 Activity의 하위 클래스는 특정 메소드를 구현하기로되어 있으므로이를 수행하기 위해 부모 클래스의 메소드를 정확하게 일치시켜 특정 메소드를 대체해야합니다. onCreate()이 그러한 방법 중 하나입니다.

에뮬레이터의 경우 GPS는 here 가이드를 따라 테스트 할 수 있습니다. 그렇지 않으면 disabled로 표시됩니다.

+0

음. 예? 그 코드의 두 번째 줄입니다. 너의 요점이 뭐야? 나는이 "벽"에서 새로운 사람이라는 것을 기억하십시오. – Erik

+0

미안하지만, 나는 재미있어하려고 애썼다. 편집을 참조하십시오. – Jasoneer

+0

@ErikEdgren 'onCreate1()'이 아니라'onCreate()'이어야합니다 (뒤에'1'이 있습니다). 그러나, 실제로 후행하는'1'을 가진 메소드를 선언하고자한다면 위의 응답을 무시하십시오. 그러나 onCreate()는 Android의 모든 Activies에서 중요한 메소드이므로 자동으로 호출됩니다. :) – ninetwozero

관련 문제