2012-04-02 7 views
0

私はEclipseで新しく、Androidアプリケーションは非常に新人の質問になります。この機能を正しく機能させるにはどうすればよいですか?私はちょうどコピーして、私の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()はこのような方法の1つです。

エミュレータの場合、GPSは、hereのガイドに従ってテストすることができます。それ以外の場合は無効として表示されます。

+0

Um。はい?それがそのコードの2行目です。あなたのポイントは何ですか?私はこれらの "壁"の中で新しいです、覚えています。 – Erik

+0

申し訳ありませんが、私は面白くしようとしていました。編集を参照してください。 – Jasoneer

+0

@ErikEdgren 'onCreate()'ではなく 'onCreate()'でなければなりません(末尾の '1'に注意してください)。しかし、実際に '1'という末尾のメソッドを宣言しようとするならば、上記の応答は無視してください。しかし、onCreate()はAndroidのすべてのActiviesにとって重要なメソッドなので、自動的に呼び出されます。 :) – ninetwozero