質問
下に載せてあるコードを実行すると、以下のエラーが表示されました。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.myapplication.MainActivity.<init>(MainActivity.java:25)
載せてあるプログラムの説明を簡単にすると、
1、地図の画像をImageViewを用いて張り付ける。
2、その画像の左上、右下の座標をもとにGPS情報を用いて、自分の現在地が表示されるようにdpと緯度経度の計算式を作る。
3、GPS情報の取得
4、計算式、GPS情報から自分の現在地をマーカーのImageViewを貼り付ける。
というプログラムです。
調べてみると、おそらくnullというコードが関係していると思われるのですが、解決方法がわかりません。
プログラム自体にエラーは出ていないのですが、実行すると実機、エミュレーターともにアプリが落ちてしまいます。
どのようにすればエラーがなくなる可能性があるか、教えていただけないでしょうか。
Java
1import android.support.annotation.NonNull; 2import android.support.v4.content.ContextCompat; 3import android.support.v7.app.AppCompatActivity; 4import android.os.Bundle; 5import android.content.pm.PackageManager; 6import android.location.Location; 7import android.location.LocationListener; 8import android.location.LocationManager; 9import android.location.LocationProvider; 10import android.support.v4.app.ActivityCompat; 11import android.widget.ImageView; 12import android.widget.TextView; 13import android.content.Intent; 14import android.provider.Settings; 15import android.util.Log; 16import android.widget.Toast; 17import android.Manifest; 18 19public class MainActivity extends AppCompatActivity implements LocationListener{ 20 21 private Location location1; 22 23 double Lati =location1.getLatitude(); 24 double Longi =location1.getLongitude(); 25 26 double a=340; 27 double b=33.104001; 28 double c=131.303325; 29 double d=300; 30 double e=0.001823; 31 double f=0.002557; 32 double g= location1.getLatitude(); 33 double h= location1.getLongitude(); 34 double i=b - g; 35 double j=h - c; 36 double k=a * i / e; 37 double l=d * j / f; 38 39 40 LocationManager locationManager; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.activity_main); 46 47 ImageView imageView =findViewById(R.id.image_view); 48 imageView.setTranslationX((float) k); 49 imageView.setTranslationY((float) l); 50 51 52 if (ContextCompat.checkSelfPermission(this, 53 Manifest.permission.ACCESS_FINE_LOCATION) != 54 PackageManager.PERMISSION_GRANTED) { 55 ActivityCompat.requestPermissions(this, 56 new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 57 1000); 58 } else { 59 locationStart(); 60 61 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 62 1000, 50, this); 63 64 } 65 } 66 67 private void locationStart() { 68 Log.d("debug", "locationStart()"); 69 70 // LocationManager インスタンス生成 71 locationManager = 72 (LocationManager) getSystemService(LOCATION_SERVICE); 73 74 if (locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 75 Log.d("debug", "location manager Enabled"); 76 } else { 77 // GPSを設定するように促す 78 Intent settingsIntent = 79 new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 80 startActivity(settingsIntent); 81 Log.d("debug", "not gpsEnable, startActivity"); 82 } 83 84 if (ContextCompat.checkSelfPermission(this, 85 Manifest.permission.ACCESS_FINE_LOCATION) != 86 PackageManager.PERMISSION_GRANTED) { 87 ActivityCompat.requestPermissions(this, 88 new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000); 89 90 Log.d("debug", "checkSelfPermission false"); 91 return; 92 } 93 94 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 95 1000, 50, this); 96 97 } 98 99 // 結果の受け取り 100 101 /** 102 * Android Quickstart: 103 * https://developers.google.com/sheets/api/quickstart/android 104 * <p> 105 * Respond to requests for permissions at runtime for API 23 and above. 106 * 107 * @param requestCode The request code passed in 108 * requestPermissions(android.app.Activity, String, int, String[]) 109 * @param permissions The requested permissions. Never null. 110 * @param grantResults The grant results for the corresponding permissions 111 * which is either PERMISSION_GRANTED or PERMISSION_DENIED. Never null. 112 */ 113 @Override 114 public void onRequestPermissionsResult( 115 int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 116 if (requestCode == 1000) { 117 // 使用が許可された 118 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 119 Log.d("debug", "checkSelfPermission true"); 120 121 locationStart(); 122 123 } else { 124 // それでも拒否された時の対応 125 Toast toast = Toast.makeText(this, 126 "これ以上なにもできません", Toast.LENGTH_SHORT); 127 toast.show(); 128 } 129 } 130 } 131 132 @Override 133 public void onStatusChanged(String provider, int status, Bundle extras) { 134 switch (status) { 135 case LocationProvider.AVAILABLE: 136 Log.d("debug", "LocationProvider.AVAILABLE"); 137 break; 138 case LocationProvider.OUT_OF_SERVICE: 139 Log.d("debug", "LocationProvider.OUT_OF_SERVICE"); 140 break; 141 case LocationProvider.TEMPORARILY_UNAVAILABLE: 142 Log.d("debug", "LocationProvider.TEMPORARILY_UNAVAILABLE"); 143 break; 144 } 145 } 146 147 @Override 148 public void onLocationChanged(Location location) { 149 150 location1 = location; 151 152 } 153 154 @Override 155 public void onProviderEnabled(String provider) { 156 157 } 158 159 @Override 160 public void onProviderDisabled(String provider) { 161 162 } 163}
activity_main
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.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 <FrameLayout 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 > 13 14 15 <ImageView 16 android:layout_width="340dp" 17 android:layout_height="300dp" 18 android:src="@drawable/tizu4" 19 app:layout_constraintEnd_toEndOf="parent" 20 app:layout_constraintLeft_toLeftOf="parent" 21 app:layout_constraintRight_toRightOf="parent" 22 app:layout_constraintStart_toStartOf="parent" 23 app:layout_constraintTop_toTopOf="parent" /> 24 25 26 <ImageView 27 android:layout_width="15dp" 28 android:layout_height="15dp" 29 android:id="@+id/image_view" 30 android:src="@drawable/azaz" 31 /> 32 33 </FrameLayout> 34</android.support.constraint.ConstraintLayout>
AndroidManifest
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.sousaku1.nakahara.myapplication"> 4 5 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 6 7 <application 8 android:allowBackup="true" 9 android:icon="@mipmap/ic_launcher" 10 android:label="@string/app_name" 11 android:roundIcon="@mipmap/ic_launcher_round" 12 android:supportsRtl="true" 13 android:theme="@style/AppTheme"> 14 <activity android:name=".MainActivity"> 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 </application> 22 23</manifest>
回答2件
あなたの回答
tips
プレビュー