実現したいこと
クリックハンドラーで画面遷移をさせたい
・ボタンタップ→画面遷移
・MainActivity→AddActivityに遷移
発生している問題・エラーメッセージ
特にエラーコードは発生しておらず、ボタンをタップしても何も起こりません。
必要な記述は試しましたが、何が原因で動かないのか分からないので、教えて頂きたいです。
試したこと
・MainActivityにsetOnClickListenerを実装、Intentで画面遷移
・AndroidManifestにactivityの追加
該当のソースコード
▼MainActivity
Kotlin
package com.example.showtextonmapapp import省略 class MainActivity : AppCompatActivity(), OnMapReadyCallback { private lateinit var mMap: GoogleMap private val MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 1 private lateinit var fusedLocationProviderClient : FusedLocationProviderClient private lateinit var lastLocation: Location private var locationCallback : LocationCallback? = null private lateinit var realm: Realm override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //画面をスリープにしない window.addFlags(FLAG_KEEP_SCREEN_ON) // Obtain the SupportMapFragment and get notified when the map is ready to be used. val mapFragment = supportFragmentManager .findFragmentById(R.id.map) as SupportMapFragment mapFragment.getMapAsync(this) fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this) realm = Realm.getDefaultInstance() val toAddBtn: Button = findViewById(R.id.toAddBtn) toAddBtn.setOnClickListener { val intent = Intent(this, AddActivity::class.java) intent.putExtra("lat", lastLocation.latitude) intent.putExtra("lng", lastLocation.longitude) startActivity(intent) } } override fun onMapReady(googleMap: GoogleMap) { mMap = googleMap checkPermission() } private fun checkPermission() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { myLocationEnable() } else { requestLocationPermission() } } private fun requestLocationPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { //許可が拒否された場合 ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION) } else { //許可も求めていない場合 ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION) } } override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String>, grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) when(requestCode) { MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION->{ if (permissions.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //許可された myLocationEnable() } else { showToast("現在位置は表示できません") } } } } private fun myLocationEnable() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mMap.isMyLocationEnabled = true val locationRequest = LocationRequest().apply { interval = 10000 fastestInterval = 5000 priority = LocationRequest.PRIORITY_HIGH_ACCURACY } locationCallback = object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult?) { val textView : TextView = findViewById(R.id.textView) if (locationResult?.lastLocation != null) { lastLocation = locationResult.lastLocation val currentLatLng = LatLng(lastLocation.latitude, lastLocation.longitude) mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLatLng)) textView.text = "Lat:${lastLocation.latitude}, Lng:${lastLocation.longitude}" } } } fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null) } } private fun showToast(msg: String) { val toast = Toast.makeText(this, msg, Toast.LENGTH_LONG) toast.show() } override fun onPause() { super.onPause() if (locationCallback != null) { fusedLocationProviderClient.removeLocationUpdates(locationCallback) } } override fun onDestroy() { super.onDestroy() realm.close() } }
▼AddActivity.kt
Kotlin
package com.example.showtextonmapapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.Toast import io.realm.Realm import io.realm.kotlin.createObject import io.realm.kotlin.where import java.util.* class AddActivity : AppCompatActivity() { private lateinit var realm: Realm override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_add) realm = Realm.getDefaultInstance() val lat = intent.getDoubleExtra("lat", 0.0) val lng = intent.getDoubleExtra("lng", 0.0) val saveBtn: Button = findViewById(R.id.saveBtn) val cancelBtn: Button = findViewById(R.id.cancelBtn) saveBtn.setOnClickListener { val editText: EditText = findViewById(R.id.editText) val shopInfoStr = editText.text?.toString() ?: "" realm.executeTransaction { val maxId = realm.where<ShopInfo>().max("id") val nextId = (maxId?.toLong() ?: 0L) + 1L val shopInfo = realm.createObject<ShopInfo>(nextId) shopInfo.dateTime = Date() shopInfo.lat = lat shopInfo.lng = lng shopInfo.shopInfo = shopInfoStr } showToast("保存しました") finish() } cancelBtn.setOnClickListener {} finish() } override fun onDestroy() { super.onDestroy() realm.close() } private fun showToast(msg: String) { val toast = Toast.makeText(this, msg, Toast.LENGTH_LONG) toast.show() } }
▼activity_add.xml
Kotlin
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AddActivity"> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="100dp" android:layout_marginTop="150dp" android:layout_marginEnd="100dp" android:ems="10" android:inputType="textPersonName" android:text="" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/saveBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="100dp" android:layout_marginTop="250dp" android:text="保存" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/cancelBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="250dp" android:layout_marginEnd="100dp" android:text="キャンセル" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="テキストを入力してください" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
▼AndroidManifest.xml
Kotlin
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.showtextonmapapp"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <application android:name=".CustomApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.ShowTextOnMapApp"> <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" /> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AddActivity" /> </application> </manifest>
まだ回答がついていません
会員登録して回答してみよう