2

こんにちは、stackoverflow の世界です。

これは私の最初の投稿です。

私はAndroidの世界では新しいので、オンラインのさまざまなチュートリアルをチェックしても、自分のマップを開いたときに表示される必要があるGPS設定を有効にすることに問題があります。

ここで見つけた次のコードを試してみました: Android get location or prompt to enable location service if disabled

Button gpsButton = (Button)this.findViewById(R.id.buttonGPSLocation);
gpsButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // Start loction service
    LocationManager locationManager = (LocationManager)[OUTERCLASS].this.getSystemService(Context.LOCATION_SERVICE);

    Criteria locationCritera = new Criteria();
    locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
    locationCritera.setAltitudeRequired(false);
    locationCritera.setBearingRequired(false);
    locationCritera.setCostAllowed(true);
    locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

    String providerName = locationManager.getBestProvider(locationCritera, true);

    if (providerName != null && locationManager.isProviderEnabled(providerName)) {
        // Provider is enabled
        locationManager.requestLocationUpdates(providerName, 20000, 100, [OUTERCLASS].this.locationListener);
    } else {
        // Provider not enabled, prompt user to enable it
        Toast.makeText([OUTERCLASS].this, R.string.please_turn_on_gps, Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        [OUTERCLASS].this.startActivity(myIntent);
    }
}
});

しかし、おそらく私もそれを理解できないため、いくつかのエラーが表示されます!!!

これらは私の質問です:

  • ボタン (gpsButton) を宣言する必要があるのはなぜですか? また、どこでどのように作成できますか?
  • コードは全体的にクラス MapActivity の後に配置するか、最初の onCreate の後に配置する必要がありますか?
  • アウタークラスとは?
  • マニフェストの許可の一部 (既に持っています) レイアウトで何かを変更する必要がありますか?

したがって、一般的に言えば、このコードをどのように使用できるか、および何を変更する必要があるかを誰かがよりよく説明してくれます。

必要に応じて、Java コード全体を送信できます。

ご利用いただきありがとうございます。

よろしく、

クラウディオ

4

1 に答える 1

1

Well, you've got several questions here.

Why it's necessary to declare a Button (gpsButton) and where/how I can create it?

If you want to create a button then you can declare it in either in the XML layout used by your Activity or else you can add it in code. If you're simply pasting the code from some sample, you'll need to understand that this code actually creates a reference to a Button object by finding it in the current view hiearchy (meaning the layout currently being displayed) by it's id. So if you haven't created a button with id of buttonGPSLocation somewhere in XML, then this code won't work. As to why it's necessary: it seems like the author of the sample decided to prompt the user to tap a button in order to open the phone setting and enable GPS.

The code can be overall putted after the class MapActivity or must be putted after the first onCreate?

This question isn't very clear (might be a language issue here), but it might help if you post all the relevant code. In general, setting onClickListeners should probably be done in onCreate of your Activity. Whether it's done inside of a MapActivity or not depends on more information that you haven't supplied to us.

What is OUTERCLASS?

In Java, you can define a class inside of another class. We usually see classes defined as "top level" classes, i.e. some file names SomeClass.java, the contents of which look like

public class SomeClass {
//some code
}

Now, when you define a class inside of another class, the "nested" one is the inside of the other and not vice versa. So the one that contains the nested class is the "outer" class.

class OuterClass {
    ...
    class NestedClass {
        ...
    }
}

Why does this all matter? Well because there are some rules about how things work between outer and nested classes. In your case what's most relevant is the fact when you set an onClickListener on an button, what you're actually doing is passing a reference to an instance of the OnClickListener class. In Java, you can create an anonymous inner class, which means that instead of defining some top level class that implements onClickListener (which btw, you can easily by having the class that all of this is happening in simply declare that it implements the onClickListener interface, e.g. public class WhateverMyClassNameIs extends Activity implements View.OnClickListener) you are actually just creating an unnamed, on the fly onClickListener instance which cannot be referenced anywhere else because you haven't declared a reference to it. Now, the issue is that inside of this onClicklistener instance, when you want to now refer to the outer class that it's defined in (in your case, the Activity class), you cannot just use "this" anymore since "this" is actually the onClickListener because of the way that "scope" works. In other words, an OnClickListener has no method called "startActivity"; only an Activity class does (or your class that extends Activity). So you have to use what's called the "qualified this" to clarify that you intend to reference the "outer class", which is why you need *MyActivity*.this.startActivity...

A part of the permission on Manifest(I already have) I've to change something on Layout?

The manifest and layout files are two totally different things. They are both defined using XML, but they're totally different. The manifest, among other things, contains a declaration of all of your Activity classes and permissions that the app is requesting, etc. Layout files are the actual layouts used inside of Activities (the UI). They should be located in res/layout and you can define whatever you want there. When you need to then make that UI "alive" you have to grab references to your widgets (buttons, textviews) etc inside of your Activity in code and then do what you want there.

于 2012-11-18T01:56:48.933 に答える