我是 Kotlin 新手,我想呼叫一個按鈕BTN_test,但我不能
MainActivity.kt
package com.example.testapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var btn = BTN_test
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#575757"
tools:context=".MainActivity">
<Button
android:id="@ id/BTN_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/st_btn1" />
<TextView
android:id="@ id/textView"
android:layout_width="146dp"
android:layout_height="54dp"
android:layout_alignParentTop="true"
android:layout_marginStart="150dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="150dp"
android:text="@string/st_textView"
android:textSize="15sp"
tools:ignore="SmallSp" />
</RelativeLayout>
我試圖將其添加到 gradle 檔案中
id 'kotlin-android'
id 'kotlin-android-extensions'
build.gradle(測驗應用)
plugins {
id 'com.android.application'
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
task clean(type: Delete) {
delete rootProject.buildDir
}
但是出現了這個錯誤
Build file 'C:\Users\suhaib\AndroidStudioProjects\testapp\build.gradle' line: 3
Plugin [id: 'com.android.application'] was not found in any of the following sources:
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'com.android.application'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolveToFoundResult(DefaultPluginRequestApplicator.java:222)
at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.lambda$resolvePluginRequests$4(DefaultPluginRequestApplicator.java:148)...
也會出現這個錯誤
Gradle sync failed: Plugin [id: 'com.android.application'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source) (2 s 524 ms)
關于安卓作業室
Android Studio Bumblebee | 2021.1.1 Patch 3
Build #AI-211.7628.21.2111.8309675, built on March 16, 2022
Runtime version: 11.0.11 9-b60-7590822 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 4
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: org.jetbrains.kotlin (211-1.6.21-release-334-AS7442.40)
筆記
我設法解決了問題,我將把這個問題留給其他人受益
解決方案
我把插件放在錯誤的地方我應該把它寫在build.gradle(:app)而不是build.gradle(test app)
所以build.gradle(:app)就是這樣
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.testapp"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
thank you ^_^
uj5u.com熱心網友回復:
在 Android 中使用視圖時,您不能直接參考在 xml 布局檔案中定義的元素。Activity 類具有findViewById()您應該使用的方法,如下所示。
val btn = findViewById(R.id.BTN_test)
順便。您不知道這一點的事實已經表明您錯過了 Android 學習的幾個基本步驟。我強烈建議您回傳并從頭開始,因為從長遠來看,這將為您省去很多麻煩。findViewById()官方培訓是一個很好的起點,它會教給您比其他重要資訊更好的參考觀點的方法。祝你好運
https://developer.android.com/courses/android-basics-kotlin/unit-1
uj5u.com熱心網友回復:
我推薦你使用系結結構,它更具創新性并且可以為你節省大量代碼。
構建gradle(模塊)
buildFeatures {
viewBinding true
dataBinding true
}
主要活動
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.apply{
val btn=BTNTest
}
}
}
uj5u.com熱心網友回復:
按鈕按鈕 = (Button)findViewById(R.id.BTN_test);
uj5u.com熱心網友回復:
為了在 MainActivity.kt 中使用視圖,您不僅必須參考視圖本身,還必須參考按鈕可以做什么。您確實在 UI 設計中創建了一個按鈕,但是讓按鈕在單擊后執行某些操作。你來做這件事:
你在 MainActivity.kt 的 onCreate() 中輸入這個
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var btn = findViewById<Button>(R.id.BTN_test)
btn.setOnClickListener {
//Do something after the button is clicked
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/471537.html
