Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

アプリケーションが即終了してしまう

現在位置を取得するアプリケーションを作ろうとしていますが、
現在アプリを起動するとすぐに終了していまいます。

同じ事象になった方等いらっしゃいましたら、解決策をお願いいたします。

発生している問題・エラー

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

2Answer

クラッシュしたときにスタックトレースが出力されているはずなので確認してみてください。

0Like

Comments

  1. @daifu9mogumogu

    Questioner

    回答ありがとうございます。
    自分も見方がわるいのか、やり方が悪いのかスタックトレースに何も出力されませんでした。
    エミュレータがうまく動かないので、実機で動作確認していますが、
    実機だとスタックトレースは吐き出されないのでしょうか?
  2. @daifu9mogumogu

    Questioner

    スタックトレースが出力する設定を調べてみます。
  3. @daifu9mogumogu

    Questioner

    スタックトレースについては調べても出力方法が出てこなかったので諦めました。
    一旦下記個所を削除し、一部を交換しました。
    強制終了はなくなり、位置情報の許可状態によって条件分岐が働いていることは
    確認できましたが、位置情報の取得はできていない状態です。

    <削除部分>
    LocationManager Ichi = (LocationManager) getSystemService(LOCATION_SERVICE);
    Location location = Ichi.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    <交換前>
    String str1 = "Latitude:" + location.getLatitude();
    ((TextView) findViewById(R.id.textView2)).setText(str1);
    String str2 = "Longitude:" + location.getLongitude();
    ((TextView) findViewById(R.id.textView4)).setText(str2);

    <交換後>
    ((TextView) findViewById(R.id.textView4)).setText("Helllo");
  4. 位置情報の許可の他にプロバイダの使用可否もチェックが必要かもしれませんね。

Your answer might help someone💌