アプリケーションが即終了してしまう
Q&A
Closed
現在位置を取得するアプリケーションを作ろうとしていますが、
現在アプリを起動するとすぐに終了していまいます。
同じ事象になった方等いらっしゃいましたら、解決策をお願いいたします。
発生している問題・エラー
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`
String literal in `setText` can not be translated. Use Android resources instead.
String literal in `setText` can not be translated. Use Android resources instead.
String literal in `setText` can not be translated. Use Android resources instead.
String literal in `setText` can not be translated. Use Android resources instead.
Typo: In word 'Ichi'
MainActivity.java
package com.example.a004_test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.location.LocationManager;
import android.location.Location;
public class MainActivity extends AppCompatActivity {
LocationManager Ichi = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = Ichi.getLastKnownLocation(LocationManager.GPS_PROVIDER);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void set(View view) {
((TextView) findViewById(R.id.textView1)).setText("Hello");
((TextView) findViewById(R.id.textView2)).setText("Hello");
((TextView) findViewById(R.id.textView3)).setText("Hello");
((TextView) findViewById(R.id.textView4)).setText("Hello");
int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck == 0){
String str1 = "Latitude:" + location.getLatitude();
((TextView) findViewById(R.id.textView2)).setText(str1);
String str2 = "Longitude:" + location.getLongitude();
((TextView) findViewById(R.id.textView4)).setText(str2);
}
}
public void reset(View view){
((TextView)findViewById(R.id.textView1)).setText("");
((TextView)findViewById(R.id.textView2)).setText("");
((TextView)findViewById(R.id.textView3)).setText("");
((TextView)findViewById(R.id.textView4)).setText("");
}
}
0