#android
#Android
Вопрос:
Привет, ребята. Поэтому, когда я пытаюсь нажать кнопку «Ввод» на изображении выше, Android отключается из-за того, что что-то происходит в функции onCreate, в которой я не уверен, я протестировал пустую страницу с помощью кнопки выше, и она работает отлично; Я знаю, что это связано с кодом внутрифункция onCreate, есть идеи, как я могу это исправить? Спасибо за любую помощь, вот весь код:
(хост в main_activity)
Метод onCreate для калькулятора:
class calcFragment : AppCompatActivity() {
lateinit var operation: String
var sNumber: Double = 0.0
var fnumber: Double = 0.0
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_calc)
dot.setOnClickListener {
val oldvalue = output.text.toString()
val set = "."
output.text = oldvalue set
}
zero.setOnClickListener {
val oldvalue = output.text.toString()
val set = "0"
output.text = oldvalue set
}
one.setOnClickListener {
val oldvalue = output.text.toString()
val set = "1"
output.setText(oldvalue set)
}
two.setOnClickListener {
val oldvalue = output.text.toString()
val set = "2"
output.setText(oldvalue set)
}
three.setOnClickListener {
val oldvalue = output.text.toString()
val set = "3"
output.text = oldvalue set
}
four.setOnClickListener {
val oldvalue = output.text.toString()
val set = "4"
output.text = oldvalue set
}
five.setOnClickListener {
val oldvalue = output.text.toString()
val set = "5"
output.text = oldvalue set
}
six.setOnClickListener {
val oldvalue = output.text.toString()
val set = "6"
output.text = oldvalue set
}
seven.setOnClickListener {
val oldvalue = output.text.toString()
val set = "7"
output.text = oldvalue set
}
eight.setOnClickListener {
val oldvalue = output.text.toString()
val set = "8"
output.text = oldvalue set
}
nine.setOnClickListener {
val oldvalue = output.text.toString()
val set = "9"
output.setText(oldvalue set)
}
plus.setOnClickListener {
var value = output.text.toString()
Log.i("x", "This is $value")
var number = value.toDouble()
fnumber = number
output.text = ""
operation = " "
}
subtract.setOnClickListener {
val value = output.text.toString()
val number = value.toDouble()
fnumber = number
output.text = ""
operation = "-"
}
multiplication.setOnClickListener {
val value = output.text.toString()
val number = value.toDouble()
fnumber = number
output.setText("")
operation = "*"
}
division.setOnClickListener {
val value = output.text.toString()
val number = value.toDouble()
fnumber = number
output.setText("")
operation = "/"
}
mod.setOnClickListener {
val value = output.text.toString()
val number = value.toDouble()
fnumber = number
output.setText("")
operation = "%"
}
equals.setOnClickListener {
try {
if (operation == " ") {
val value = output.text.toString()
sNumber = value.toDouble()
var system = fnumber sNumber
input.text = "$fnumber $sNumber"
output.text = system.toString()
val result = fnumber sNumber
output.text = result.toString()
} else if (operation == "-") {
val value = output.text.toString()
sNumber = value.toDouble()
var system = fnumber - sNumber
input.text = "$fnumber - $sNumber"
output.text = system.toString()
val result = fnumber - sNumber
output.text = result.toString()
} else if (operation == "*") {
val value = output.text.toString()
sNumber = value.toDouble()
var system = fnumber * sNumber
input.text = "$fnumber * $sNumber"
output.text = system.toString()
val result = fnumber * sNumber
output.text = result.toString()
} else if (operation == "/") {
val value = output.text.toString()
sNumber = value.toDouble()
if (sNumber.equals(0)) {
val no = "CAN'T DIVIDE BY ZERO"
output.text = no
} else {
val value = output.text.toString()
sNumber = value.toDouble()
var system = fnumber / sNumber
output.text = system.toString()
input.text = "$fnumber / $sNumber"
val oldoutP = output.text.toString()
oldoutP.toDouble()
val result = fnumber / sNumber
output.text = result.toString()
}
} else if (operation == "%") {
val value = output.text.toString()
sNumber = value.toDouble()
var system = fnumber % sNumber
output.text = system.toString()
val result = fnumber % sNumber
output.text = result.toString()
}
}
catch (e: ArithmeticException){
println("pressed enter without a value")
}
}
Это код для моего навигационного графика:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/main_navigation"
app:startDestination="@id/loginFragment">
<fragment
android:id="@ id/calcFragment"
android:name="com.example.projectone.calcFragment"
android:label="fragment_calc"
tools:layout="@layout/fragment_calc" />
<fragment
android:id="@ id/loginFragment"
android:name="com.example.projectone.loginFragment"
android:label="fragment_login"
tools:layout="@layout/fragment_login" >
<action
android:id="@ id/action_loginFragment_to_calcFragment"
app:destination="@id/calcFragment" />
</fragment>
</navigation>
Вот файл журнала:
> 10/19 11:14:00: Launching 'app' on Pixel 3a API 30.
$ adb shell am start -n "com.example.projectone/com.example.projectone.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 15505 on device 'Pixel_3a_API_30 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/mple.projecton: Not late-enabling -Xcheck:jni (already on)
I/mple.projecton: Unquickening 12 vdex files!
W/mple.projecton: Unexpected CPU variant for X86 using defaults: x86
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
W/mple.projecton: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/HostConnection: HostConnection::get() New Host Connection established 0xefbd2df0, tid 15533
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/EGL_emulation: eglCreateContext: 0xef9ded20: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xef9ded20: ver 2 0 (tinfo 0xefd39f70) (first time)
I/Gralloc4: mapper 4.x is not supported
D/HostConnection: createUnique: call
D/HostConnection: HostConnection::get() New Host Connection established 0xefbd4050, tid 15533
D/goldfish-address-space: allocate: Ask for block of size 0x100
allocate: ioctl allocate returned offset 0x3fa6c3000 size 0x2000
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.projectone, PID: 15505
java.lang.ClassCastException: com.example.projectone.calcFragment cannot be cast to androidx.fragment.app.Fragment
at androidx.fragment.app.Fragment.instantiate(Fragment.java:548)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:390)
at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:132)
at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:162)
at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58)
at androidx.navigation.NavController.navigate(NavController.java:1049)
at androidx.navigation.NavController.navigate(NavController.java:935)
at androidx.navigation.NavController.navigate(NavController.java:868)
at androidx.navigation.NavController.navigate(NavController.java:854)
at androidx.navigation.NavController.navigate(NavController.java:842)
at com.example.projectone.loginFragment$onViewCreated$1$1.onClick(loginFragment.kt:49)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 15505 SIG: 9
Комментарии:
1. Можете ли вы опубликовать сообщение в журнале отладчика?
2. Для начала,
calcFragment
это не фрагмент, а действие.3. @LucaPizzini Я только что добавил журнал
4. Начните с первого шага. Выясните, как работать с фрагментами.