我已經在編譯器上做過所有的拼字檢查,並且也請chatGPT幫我檢查過文法錯誤
但都找不到為什麼啟動的時候會閃退
請問我到底在哪裡做錯了呢?
MAinActivity.java
package com.jismayProject.javatoandroid;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
//import android.support.v7.app.AppComattActivity;
import java.util.HashSet;
public class MainActivity extends AppCompatActivity {
private TextView lottery;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lottery = (TextView)findViewById(R.id.lottery);
}
public void createLottery(View view){
HashSet<Integer> set = new HashSet<>();
while (set.size()<6){
set.add((int)(Math.random() * 49 + 1));
}
lottery.setText(set.toString());
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.jismayProject.javatoandroid.MainActivity"
android:orientation="vertical">
<!--灰色區塊表示沒有被使用到 -->
<!-- 這邊開始是新增的功能 -->
<Button
android:layout_width="match_parent"
android:layout_height="123dp"
android:onClick="createLottery"
android:text="出樂透" />
<TextView
android:id="@+id/lottery"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:text="Hello World"
android:textSize="20sp" />
</LinearLayout>
我也想過是否是引用或依賴錯誤的問題,但找不到錯誤點
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">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.JavaToAndroid"
tools:targetApi="34">
<activity
android:name="MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.JavaToAndroid">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle.kts
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.jismayProject.javatoandroid"
compileSdk = 34
defaultConfig {
applicationId = "com.jismayProject.javatoandroid"
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
release {
isMinifyEnabled = 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"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.3"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
implementation("androidx.appcompat:appcompat:1.6.1")
}
----------------已解決----------------
引 我使用這本書 "從java到android行動裝置程式設計必修的15堂課 ISBM 978-986-199-484-0)
開發環境與建立新專案 章節的最後練習
原來問題發生在 themes.xml 與MainActivity.app的調用不一致,
在跟隨課本內容指示去操作會得到上面java的程式碼,
但在public class MainActivity 裡繼承的 AppCompatActivity,
無法調用本來在編輯器中預設的app/res/themes.xml > style name="Theme.JavaToAndroid" parent="Theme.Material.Light.NoActionBar" ,
根據chatGPT提示,將其更改成AppCompatActivity裡的Theme.AppCompat.Light.DarkActionBar,
程式就能正常運行了.