質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
GPS

GPSは、Global Positioning Systemの略です。衛星信号を使用して受信機の地上又は空中内の居場所を特定するナビゲーションシステムです。"GPS"は受信機のことも指します。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

3375閲覧

Android端末(実機)で位置情報を取得し表示できません

gken

総合スコア4

GPS

GPSは、Global Positioning Systemの略です。衛星信号を使用して受信機の地上又は空中内の居場所を特定するナビゲーションシステムです。"GPS"は受信機のことも指します。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2021/09/15 03:50

編集2021/09/15 03:51

Android端末で位置情報を取得し表示させたいです。

実機を使用して学習をしています。
Nexus7 Android ver6.0.1 を使用して位置情報取得させたく以下のサイトに載っているコードを使用しています。

"[Android] GPSで位置情報を取得するアプリを作る"
https://akira-watson.com/android/gps.html

最小SDK API23:Android 6.0 を使用しています。

現状3つの質問があり対処方法を教えて頂きたいです。

  • 実行前からMainActivity.javaの93行目に

Overriding method should call super.onRequestPermissionsResult 
という問題が出ます。

  • MainActivity.javaの111行目のonLocationChangedメソッドがいつ呼び出されるのかが分かりません。
  • 実行後 実機の画面に何も表示されません。

実行前のエラー MainActivity.java 93行目 

93

1Overriding method should call `super.onRequestPermissionsResult`

Logcat

Logcat

12021-09-15 12:14:26.755 31863-31863/com.example.point W/System: ClassLoader referenced unknown path: /data/app/com.example.point-1/lib/arm 22021-09-15 12:14:26.803 31863-31863/com.example.point W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 32021-09-15 12:14:26.934 31863-31863/com.example.point D/debug: locationStart() 42021-09-15 12:14:26.937 31863-31863/com.example.point D/debug: location manager Enabled 52021-09-15 12:14:26.952 31863-31886/com.example.point D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 62021-09-15 12:14:26.986 31863-31886/com.example.point I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb 72021-09-15 12:14:26.988 31863-31886/com.example.point I/OpenGLRenderer: Initialized EGL, version 1.4 8

該当のソースコード

MainActivity.java

MainActivity

1package com.example.point; 2 3import androidx.appcompat.app.AppCompatActivity; 4import androidx.core.app.ActivityCompat; 5import androidx.core.content.ContextCompat; 6import androidx.annotation.NonNull; 7 8import android.annotation.SuppressLint; 9import android.os.Bundle; 10import android.content.pm.PackageManager; 11import android.location.Location; 12import android.location.LocationListener; 13import android.location.LocationManager; 14import android.location.LocationProvider; 15import android.widget.TextView; 16import android.content.Intent; 17import android.provider.Settings; 18import android.util.Log; 19import android.widget.Toast; 20import android.Manifest; 21 22public class MainActivity extends AppCompatActivity implements LocationListener{ 23 24 LocationManager locationManager; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 31 if (ContextCompat.checkSelfPermission(this, 32 Manifest.permission.ACCESS_FINE_LOCATION) != 33 PackageManager.PERMISSION_GRANTED) { 34 ActivityCompat.requestPermissions(this, 35 new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 36 1000); 37 }else{ 38 39 locationStart(); 40 41 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 42 1000, 50, this); 43 } 44 45 } 46 47 private void locationStart(){ 48 Log.d("debug","locationStart()"); 49 50 // LocationManager インスタンス生成 51 locationManager = 52 (LocationManager) getSystemService(LOCATION_SERVICE); 53 54 if (locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 55 Log.d("debug", "location manager Enabled"); 56 } else { 57 // GPSを設定するように促す 58 Intent settingsIntent = 59 new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 60 startActivity(settingsIntent); 61 Log.d("debug", "not gpsEnable, startActivity"); 62 } 63 64 if (ContextCompat.checkSelfPermission(this, 65 Manifest.permission.ACCESS_FINE_LOCATION) != 66 PackageManager.PERMISSION_GRANTED) { 67 ActivityCompat.requestPermissions(this, 68 new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000); 69 70 Log.d("debug", "checkSelfPermission false"); 71 return; 72 } 73 74 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 75 1000, 50, this); 76 77 } 78 79 // 結果の受け取り 80 /** 81 * Android Quickstart: 82 * https://developers.google.com/sheets/api/quickstart/android 83 * 84 * Respond to requests for permissions at runtime for API 23 and above. 85 * @param requestCode The request code passed in 86 * requestPermissions(android.app.Activity, String, int, String[]) 87 * @param permissions The requested permissions. Never null. 88 * @param grantResults The grant results for the corresponding permissions 89 * which is either PERMISSION_GRANTED or PERMISSION_DENIED. Never null. 90 */ 91 92 @Override 93 public void onRequestPermissionsResult(int requestCode, @NonNull String[]permissions, @NonNull int[] grantResults) { 94 if (requestCode == 1000) { 95 // 使用が許可された 96 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 97 Log.d("debug","checkSelfPermission true"); 98 99 locationStart(); 100 101 } else { 102 // それでも拒否された時の対応 103 Toast toast = Toast.makeText(this, 104 "これ以上なにもできません", Toast.LENGTH_SHORT); 105 toast.show(); 106 } 107 } 108 } 109 110 @Override 111 public void onLocationChanged(Location location) { 112 // 緯度の表示 113 TextView textView1 = (TextView) findViewById(R.id.text_view1); 114 String str1 = "Latitude:"+location.getLatitude(); 115 Log.d("緯度", str1); 116 textView1.setText(str1); 117 118 119 // 経度の表示 120 TextView textView2 = (TextView) findViewById(R.id.text_view2); 121 String str2 = "Longtude:"+location.getLongitude(); 122 Log.d("緯度", str2); 123 textView2.setText(str2); 124 125 } 126 127 @Override 128 public void onProviderEnabled(String provider) { 129 130 } 131 132 @Override 133 public void onProviderDisabled(String provider) { 134 135 } 136}

AndroidManifest.xml

Manifest

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.point"> 4 5 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 6 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 7 8 <application 9 android:allowBackup="true" 10 android:icon="@mipmap/ic_launcher" 11 android:label="@string/app_name" 12 android:roundIcon="@mipmap/ic_launcher_round" 13 android:supportsRtl="true" 14 android:theme="@style/Theme.Point"> 15 <activity 16 android:name=".MainActivity" 17 android:exported="true"> 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 </application> 25 26</manifest>

activity_main.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/text_view1" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_margin="20dp" 14 android:textColor="#44f" 15 android:textSize="20sp" 16 app:layout_constraintBottom_toBottomOf="parent" 17 app:layout_constraintHorizontal_bias="0.501" 18 app:layout_constraintLeft_toLeftOf="parent" 19 app:layout_constraintRight_toRightOf="parent" 20 app:layout_constraintTop_toTopOf="parent" 21 app:layout_constraintVertical_bias="0.1" /> 22 23 <TextView 24 android:id="@+id/text_view2" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:textSize="20sp" 28 android:textColor="#f44" 29 android:layout_margin="20dp" 30 app:layout_constraintBottom_toBottomOf="parent" 31 app:layout_constraintHorizontal_bias="0.501" 32 app:layout_constraintLeft_toLeftOf="parent" 33 app:layout_constraintRight_toRightOf="parent" 34 app:layout_constraintTop_toTopOf="parent" 35 app:layout_constraintVertical_bias="0.2" /> 36 37</androidx.constraintlayout.widget.ConstraintLayout>

試したこと

1つ目の質問:エラー文を検索したのですが私には何が原因なのか分かりませんでした。

2、3つ目の質問:最初にLogcatを見たところMainActivity.java 55行目のLog.d();が
2021-09-15 12:14:26.937 31863-31863/com.example.point D/debug: location manager Enabled
が確認ができたため
MainActivity.javaのonLocationChangedメソッド内の115行目と125行目にLog.d();を追加し、再度実行したのですが115行目と125行目に追加したLog.d()はLogcatに表示されませんでした。そのためonLocationChangedメソッドが呼び出されていないのではないかと思いました。

補足情報

実機 Nexus7 Android 6.0.1
開発環境 Androidstudio
最小SDK API23:Android 6.0 (Marshmallow)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Overriding method should call super.onRequestPermissionsResult

Google翻訳:「オーバーライドするメソッドは super.onRequestPermissionsResultを呼び出す必要があります」


java

1public void onRequestPermissionsResult(int requestCode, @NonNull String[]permissions, @NonNull int[] grantResults) { 2 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 3 (以下略) 4}

という感じでしょうか。

まず java (というよりもプログラムの作り方?) の基本であるメソッドやパラメータというものをご理解頂いているのが大前提で、その上で「このような機能はこう使う」という記事は書かれていることがほとんどかと思います。
作られているモノに付きましても簡単な部類とは言い難いと思いますので、もう少し簡単な題材で java の練習をされてからのほうが良いのではないでしょうか。

投稿2021/09/15 05:22

編集2021/09/15 16:21
jimbe

総合スコア12648

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gken

2021/09/15 06:15

回答ありがとうございます。 onRequestPermissionsResultを super.onRequestPermissionsResultに訂正するということでしょうか. 訂正すると以下のようなエラーが出てきてしまいます。 Annotations are not allowed here:90 Identifier expected:91 Unexpected token:91 Invalid method declaration; return type required:91
jimbe

2021/09/15 06:31

java に慣れていらっしゃらないでしょうか。 「super.onRequestPermissionsResultを呼び出す」です。 onRequestPermissionsResult メソッドの最初は super.onRequestPermissionsResult を(必要なパラメータを指定して)呼び出してください。
gken

2021/09/15 10:59 編集

コメントありがとうございます。 すみません、プログラミング初学者です。 2021/09/15 15:15のコメントが間違っていると仰っているのだと思うのですが、何をすればよいのか分かりません。 public void onRequestPermissionsResult(){    super.onRequestPermissionsResult(int requestCode, @NonNull        String[]permissions, @NonNull int[] grantResults){   } } ということでしょうか。 見づらくなってしまい申し訳ありません。
gken

2021/09/16 02:43

コードを載せて頂きありがとうございます。 もう一度Javaの初級を学びながら見直したいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問