Reputation: 13
I'm trying to run an application on my phone using the "Run 'app'" function on Android Studio. The app crashes on startup and refuses to work.
I've succeded in running the app on my phone once, but it was much earlier than when this problem existed. It happened probably at an attempted fix at a compiling error, which indeed fixed it, but brings this other problem. No other question has helped me since none of them used kotlin, and I couldn't pinpoint the exact error I may have done.
Here is the logcat. (text was exceeding the 30000 character limit)
My AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="com.example.testapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" tools:ignore="GoogleAppIndexingWarning,android:appComponentFactory"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And the MainActivity.kt
:
package com.example.testapp
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
private lateinit var textMessage: TextView
private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
textMessage.setText(R.string.title_home)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
textMessage.setText(R.string.title_dashboard)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
textMessage.setText(R.string.title_notifications)
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
textMessage = findViewById(R.id.message)
navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
}}
}
Lastly, the activity_main.xml
:
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:layout_marginTop="22dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#585858"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:menu="@menu/bottom_nav_menu" app:layout_constraintEnd_toEndOf="parent"
/>
<ImageView
android:layout_width="208dp"
android:layout_height="184dp" app:srcCompat="@mipmap/image1"
android:id="@+id/imageView" android:layout_marginTop="112dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" android:contentDescription="Image 1"/>
<Button
android:text="Button"
android:layout_width="150dp"
android:layout_height="48dp"
android:id="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="36dp"/>
<ImageView
android:layout_width="151dp"
android:layout_height="153dp" app:srcCompat="@mipmap/image2"
android:id="@+id/imageView2"
android:layout_marginTop="128dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp"/>
<Button
android:text="Button1"
android:layout_width="150dp"
android:layout_height="48dp"
android:id="@+id/button2"
app:layout_constraintTop_toTopOf="@+id/button" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
If any more code is needed, I'll be sharing.
Normally, as in my first attempt way back at when I started development, the app would run normally, and wouldn't crash. Now, the app crashes instantly after being run, no matter if it's run by Android Studio or by installing an apk in the target device.
Upvotes: 0
Views: 324
Reputation: 11
in your onNavigationItemSelectedListener,you had not init TextView textMessage.
navView.setOnNavigationItemSelectedListener{ item->
when (item.itemId) {
R.id.navigation_home -> {
textMessage.setText(R.string.title_home)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
textMessage.setText(R.string.title_dashboard)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
textMessage.setText(R.string.title_notifications)
return@OnNavigationItemSelectedListener true
}
}
false
}
Upvotes: 0
Reputation: 24211
Enable the multidex support in your app/build.gradle
file.
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.3'
}
Upvotes: 1