Kotlin-Android第一个应用程序TextView按钮应用程序在点击按钮后崩溃。

huangapple go评论96阅读模式
英文:

Kotlin-Android First App TextView Button app crashed after clicking button

问题

I am new to android studio, just installed and my first app crashed after clicking the button
I added some imports but didnt work. Can anybody help ? I installed the lastest ver of andriod studio flamingo , kotlin-android-extensions didnt work anymore I think

build.gradle(:app)

  1. plugins {
  2. id 'com.android.application'
  3. id 'org.jetbrains.kotlin.android'
  4. id 'kotlin-parcelize'
  5. }
  6. android {
  7. namespace 'com.example.myapplication'
  8. compileSdk 33
  9. defaultConfig {
  10. applicationId "com.example.myapplication"
  11. minSdk 23
  12. targetSdk 33
  13. versionCode 1
  14. versionName "1.0"
  15. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  16. }
  17. buildFeatures {
  18. viewBinding true
  19. }
  20. buildTypes {
  21. release {
  22. minifyEnabled false
  23. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  24. }
  25. }
  26. compileOptions {
  27. sourceCompatibility JavaVersion.VERSION_1_8
  28. targetCompatibility JavaVersion.VERSION_1_8
  29. }
  30. kotlinOptions {
  31. jvmTarget = '1.8'
  32. }
  33. }
  34. dependencies {
  35. implementation 'androidx.core:core-ktx:1.8.0'
  36. implementation 'androidx.appcompat:appcompat:1.6.1'
  37. implementation 'com.google.android.material:material:1.5.0'
  38. implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
  39. testImplementation 'junit:junit:4.13.2'
  40. androidTestImplementation 'androidx.test.ext:junit:1.1.5'
  41. androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
  42. }

mainacitivity.kt

  1. package com.example.myapplication
  2. import android.os.Bundle
  3. import android.view.View
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.widget.TextView
  6. class MainActivity : AppCompatActivity() {
  7. private lateinit var textView : TextView
  8. override fun onCreate(savedInstanceState: Bundle?) {
  9. super.onCreate(savedInstanceState)
  10. setContentView(R.layout.activity_main)
  11. }
  12. fun degistir(view : View){
  13. textView.text = "Hi Android"
  14. }
  15. }
英文:

I am new to android studio, just installed and my first app crashed after clicking the button
I added some imports but didnt work. Can anybody help ? I installed the lastest ver of andriod studio flamingo , kotlin-android-extensions didnt work anymore I think

build.gradle(:app)

  1. plugins {
  2. id 'com.android.application'
  3. id 'org.jetbrains.kotlin.android'
  4. id 'kotlin-parcelize'
  5. }
  6. android {
  7. namespace 'com.example.myapplication'
  8. compileSdk 33
  9. defaultConfig {
  10. applicationId "com.example.myapplication"
  11. minSdk 23
  12. targetSdk 33
  13. versionCode 1
  14. versionName "1.0"
  15. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  16. }
  17. buildFeatures {
  18. viewBinding true
  19. }
  20. buildTypes {
  21. release {
  22. minifyEnabled false
  23. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  24. }
  25. }
  26. compileOptions {
  27. sourceCompatibility JavaVersion.VERSION_1_8
  28. targetCompatibility JavaVersion.VERSION_1_8
  29. }
  30. kotlinOptions {
  31. jvmTarget = '1.8'
  32. }
  33. }
  34. dependencies {
  35. implementation 'androidx.core:core-ktx:1.8.0'
  36. implementation 'androidx.appcompat:appcompat:1.6.1'
  37. implementation 'com.google.android.material:material:1.5.0'
  38. implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
  39. testImplementation 'junit:junit:4.13.2'
  40. androidTestImplementation 'androidx.test.ext:junit:1.1.5'
  41. androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
  42. }

mainacitivity.kt

  1. package com.example.myapplication
  2. import android.os.Bundle
  3. import android.view.View
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.widget.TextView
  6. class MainActivity : AppCompatActivity() {
  7. private lateinit var textView : TextView
  8. override fun onCreate(savedInstanceState: Bundle?) {
  9. super.onCreate(savedInstanceState)
  10. setContentView(R.layout.activity_main)
  11. }
  12. fun degistir(view : View){
  13. textView.text = "Hi Android"
  14. }
  15. }

答案1

得分: 0

初始化您的textView使用findviewbyId,然后尝试在其上设置文本。

英文:

Initialize your textView using findviewbyId , then try setting text on it.

huangapple
  • 本文由 发表于 2023年7月14日 04:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定