gpt4 book ai didi

java - 将“设置” Activity 连接到“Activity 2”后,我的应用程序崩溃了

转载 作者:行者123 更新时间:2023-12-01 17:55:31 25 4
gpt4 key购买 nike

因为我仍然是 Android Studio 的新手,所以我尝试使用 YouTube-Tutorial 将编码 Activity 连接到该 Activity (我上面有设置按钮的 Activity )。当我现在单击按钮时,我应该从设置 Activity 中获得常规设置。但我的应用程序崩溃了,并告诉我:不幸的是,应用程序停止了。

我不明白我做错了什么,我试了3次。我的意思是,根据教程,它必须非常简单。

<?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:background="#F4F8C8"
tools:context=".StartH" android:layout_height="match_parent">

<TextView
android:id="@+id/WelcomeH"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_marginStart="158dp"
android:layout_marginTop="45dp"
android:layout_marginEnd="157dp"
android:layout_marginBottom="649dp"
android:background="#B5651E"
android:padding="7dp"
android:text="@string/welcome_hunter"
android:textAlignment="center"
android:textColor="#FFEB3B"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CalendarView
android:id="@+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35sp"
android:layout_marginTop="150dp"
tools:layout_editor_absoluteX="33dp"
tools:layout_editor_absoluteY="143dp" />

<Button
android:id="@+id/button"
android:layout_width="102dp"
android:layout_height="48dp"
android:layout_marginStart="150dp"
android:layout_marginTop="500dp"
android:background="#B5651E"
android:text="@string/new_entry"
tools:layout_editor_absoluteX="163dp"
tools:layout_editor_absoluteY="532dp" />

<Button
android:id="@+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnSettings_onClick"
android:text="@string/settings" />

<Button
android:id="@+id/btnRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:onClick="btnRead_onClick"
android:text="@string/read_settings" />

</RelativeLayout>

(按钮位于 Activity 2 上而不是主 Activity 上)

主要 Activity :

    package com.goldenegg.hunterapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {


private MenuItem item;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

public void btnSettings_onClick(View view) {
Intent intent=new Intent (this, SettingsActivity.class);
startActivity(intent);
}

}

public void openHunter() {
Intent intent = new Intent(this, Hunter.class);
startActivity(intent);
}

public void openFisher() {
Intent intent = new Intent(this, Fisher.class);
startActivity(intent);

}

根据教程,当我单击按钮时,它已经可以工作了。我做错了什么?

感谢帮助

这是日志猫

03-17 17:47:13.289 2915-2915/? E/libprocessgroup: failed to make and chown /acct/uid_10058: Read-only file system
03-17 17:47:13.290 2915-2915/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
03-17 17:47:13.290 2915-2915/? I/art: Not late-enabling -Xcheck:jni (already on)
03-17 17:47:13.344 2915-2930/com.goldenegg.hunterapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-17 17:47:13.360 2915-2915/com.goldenegg.hunterapp D/Atlas: Validating map...
03-17 17:47:13.449 2915-2930/com.goldenegg.hunterapp I/OpenGLRenderer: Initialized EGL, version 1.4
03-17 17:47:13.449 2915-2930/com.goldenegg.hunterapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-17 17:47:13.462 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglCreateContext: 0xb42a4ac0: maj 2 min 0 rcv 2
03-17 17:47:13.479 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:13.482 2915-2930/com.goldenegg.hunterapp D/OpenGLRenderer: Enabling debug mode 0
03-17 17:47:13.499 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:18.656 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:18.687 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:18.696 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:23.891 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:23.893 2915-2915/com.goldenegg.hunterapp I/Choreographer: Skipped 311 frames! The application may be doing too much work on its main thread.
03-17 17:47:23.909 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:23.947 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:23.960 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:25.144 2915-2930/com.goldenegg.hunterapp D/EGL_emulation: eglMakeCurrent: 0xb42a4ac0: ver 2 0 (tinfo 0xb4272c70)
03-17 17:47:25.209 2915-2915/com.goldenegg.hunterapp D/AndroidRuntime: Shutting down VM
03-17 17:47:25.209 2915-2915/com.goldenegg.hunterapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.goldenegg.hunterapp, PID: 2915
java.lang.IllegalStateException: Could not find a method btnSettings_onClick(View) in the activity class com.goldenegg.hunterapp.Hunter for onClick handler on view class android.widget.Button with id 'btnSettings'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: btnSettings_onClick [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:4000)
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

最佳答案

public void btnSettings_onClick(View view) {
Intent intent=new Intent (this, SettingsActivity.class);
startActivity(intent);
}

此函数不应位于 onCreate 主体中,请将其移出并重试。

编辑:

我做了一个简单的项目来向您展示它是如何完成的。请注意,在 AndroidManifest

中添加新 Activity (设置)非常重要

AndroidManifest.xml:

<application
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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity" />
</application>

主要 Activity :

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void btnSettings_onClick(View view) {
Intent intent=new Intent (this, SettingsActivity.class);
startActivity(intent);
}
}

设置 Activity :

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}

}

为了帮助您学习 Android,您应该看看:

关于java - 将“设置” Activity 连接到“Activity 2”后,我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60725085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com