AndroidStudio 3.0开发精要Kotlin版第37章-Fragments

Android Studio 3.0 Development Essentials Kotlin Edition Chapter 37 - Fragments

这是我的第一个 Android/Kotlin 问题,也是 android 的新问题,所以请耐心等待。

在第 37 章标题中列出的 android 这本书中,当我尝试使用 "seekBar" 制作文本 bigger/smaller 时,应用程序崩溃了。我习惯了 Swift/Xcode 当应用程序崩溃时程序会直接转到导致崩溃的地方,而 android 不会那样做。

任何人都可以指出正确的方向,告诉我如何自定义 android 工作室以像 Xcode 一样工作吗?但就 "seekBar" 以下是我的代码...如果需要更多文件,请询问,我会提供。

此外,如果您查看代码底部并看到函数 "override fun onStopTrackingTouch",本书不建议在文件中实现此代码。如果我不执行此操作,则文件中存在错误。

再一次,请耐心等待我,因为我是 android 的新手,需要帮助。

package com.ebookfrenzy.fragmentexample

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.SeekBar
import android.content.Context
import kotlinx.android.synthetic.main.toolbar_fragment.*


class ToolbarFragment: Fragment(), SeekBar.OnSeekBarChangeListener {

var seekvalue = 10

var activityCallback: ToolbarFragment.ToolbarListener? = null

interface ToolbarListener {
    fun onButtonClick(position: Int, text: String)
}

override fun onAttach(context: Context?) {
    super.onAttach(context)
    try {
        activityCallback = context as ToolbarListener
    } catch (e: ClassCastException) {
        throw ClassCastException(context?.toString() + " must implement ToolbarListener")
    }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    // Inflate the layout for this fragment
    val view = inflater?.inflate(R.layout.toolbar_fragment, container, false)

    val seekBar: SeekBar? = view?.findViewById(R.id.seekBar1)
    val button: Button? = view?.findViewById(R.id.button1)

    seekBar?.setOnSeekBarChangeListener(this)

    button?.setOnClickListener { v: View -> buttonClicked(v) }

    return view
}

private fun buttonClicked(view: View) {
    activityCallback?.onButtonClick(seekvalue, editText1.text.toString())
}

override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
    seekvalue = progress
}

override fun onStartTrackingTouch(arg0: SeekBar) {

}

override fun onStopTrackingTouch(arg0: SeekBar) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

编辑Logcat输出

08-12 12:24:18.394 16587-16587/? E/Zygote: isWhitelistProcess - Process is Whitelisted
08-12 12:24:18.395 16587-16587/? E/libpersona: scanKnoxPersonas
08-12 12:24:18.396 16587-16587/? E/libpersona: Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
08-12 12:24:18.400 16587-16587/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SM-N950U_8.0.0_0009, [-1 -1 -1 -1 0 1]
08-12 12:24:18.401 16587-16587/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.ebookfrenzy.fragmentexample 
08-12 12:24:18.417 16587-16587/? I/zygote64: Late-enabling -Xcheck:jni
08-12 12:24:18.452 16587-16587/? D/TimaKeyStoreProvider: TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service
08-12 12:24:18.453 16587-16587/? D/ActivityThread: Added TimaKeyStore provider
08-12 12:24:18.501 16587-16587/com.ebookfrenzy.fragmentexample I/zygote64: no shared libraies, dex_files: 1
08-12 12:24:18.588 16587-16613/com.ebookfrenzy.fragmentexample I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/egl/libEGL_adreno.so from the current namespace instead.
08-12 12:24:18.591 16587-16613/com.ebookfrenzy.fragmentexample D/libEGL: loaded /vendor/lib64/egl/libEGL_adreno.so
08-12 12:24:18.596 16587-16587/com.ebookfrenzy.fragmentexample I/InstantRun: starting instant run server: is main process
08-12 12:24:18.600 16587-16613/com.ebookfrenzy.fragmentexample I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/egl/libGLESv1_CM_adreno.so from the current namespace instead.
08-12 12:24:18.600 16587-16613/com.ebookfrenzy.fragmentexample D/libEGL: loaded /vendor/lib64/egl/libGLESv1_CM_adreno.so
08-12 12:24:18.788 16587-16619/com.ebookfrenzy.fragmentexample D/OpenGLRenderer: HWUI GL Pipeline
08-12 12:24:18.807 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: setView = DecorView@43a999e[FragmentExampleActivity] TM=true MM=false
08-12 12:24:18.829 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: dispatchAttachedToWindow
08-12 12:24:18.836 16587-16613/com.ebookfrenzy.fragmentexample I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/egl/libGLESv2_adreno.so from the current namespace instead.
08-12 12:24:18.842 16587-16613/com.ebookfrenzy.fragmentexample D/libEGL: loaded /vendor/lib64/egl/libGLESv2_adreno.so
08-12 12:24:18.862 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: Relayout returned: old=[0,0][0,0] new=[0,0][1440,2960] result=0x7 surface={valid=true 542086074368} changed=true
08-12 12:24:18.862 16587-16619/com.ebookfrenzy.fragmentexample I/Adreno: QUALCOMM build                   : 7780dff, Iaaa4d6d123
    Build Date                       : 01/17/18
    OpenGL ES Shader Compiler Version: EV031.22.00.01
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.6.4.R1.08.00.00.309.049
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
08-12 12:24:18.868 16587-16619/com.ebookfrenzy.fragmentexample I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
08-12 12:24:18.884 16587-16619/com.ebookfrenzy.fragmentexample I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
08-12 12:24:18.891 16587-16619/com.ebookfrenzy.fragmentexample I/OpenGLRenderer: Initialized EGL, version 1.4
08-12 12:24:18.891 16587-16619/com.ebookfrenzy.fragmentexample D/OpenGLRenderer: Swap behavior 2
08-12 12:24:18.897 16587-16619/com.ebookfrenzy.fragmentexample D/libGLESv1: STS_GLApi : DTS, ODTC are not allowed for Package : com.ebookfrenzy.fragmentexample
08-12 12:24:18.899 16587-16619/com.ebookfrenzy.fragmentexample D/OpenGLRenderer: eglCreateWindowSurface = 0x7e2b725030
08-12 12:24:18.912 16587-16619/com.ebookfrenzy.fragmentexample D/OpenGLRenderer: eglDestroySurface = 0x7e2b725030
08-12 12:24:18.923 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: Relayout returned: old=[0,0][1440,2960] new=[0,0][1440,2960] result=0x5 surface={valid=false 0} changed=true
08-12 12:24:21.055 16587-16587/com.ebookfrenzy.fragmentexample W/zygote64: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
08-12 12:24:29.651 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: Relayout returned: old=[0,0][1440,2960] new=[0,0][1440,2960] result=0x7 surface={valid=true 542086074368} changed=true
08-12 12:24:29.668 16587-16619/com.ebookfrenzy.fragmentexample D/OpenGLRenderer: eglCreateWindowSurface = 0x7e2b725030
08-12 12:24:29.680 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 84 - 0, 168) vi=Rect(0, 84 - 0, 168) or=1
08-12 12:24:29.702 16587-16619/com.ebookfrenzy.fragmentexample I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
08-12 12:24:29.706 16587-16779/com.ebookfrenzy.fragmentexample E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
08-12 12:24:29.706 16587-16779/com.ebookfrenzy.fragmentexample I/chatty: uid=10229(u0_a229) hwuiTask1 identical 2 lines
08-12 12:24:29.706 16587-16779/com.ebookfrenzy.fragmentexample E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
08-12 12:24:29.706 16587-16780/com.ebookfrenzy.fragmentexample E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
    JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
08-12 12:24:29.706 16587-16779/com.ebookfrenzy.fragmentexample E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
08-12 12:24:29.706 16587-16780/com.ebookfrenzy.fragmentexample E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
08-12 12:24:29.788 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: MSG_WINDOW_FOCUS_CHANGED 1
08-12 12:24:29.809 16587-16587/com.ebookfrenzy.fragmentexample I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
08-12 12:24:29.836 16587-16587/com.ebookfrenzy.fragmentexample I/AssistStructure: Flattened final assist data: 2060 bytes, containing 1 windows, 7 views
08-12 12:24:36.825 16587-16592/com.ebookfrenzy.fragmentexample I/zygote64: Do partial code cache collection, code=29KB, data=27KB
08-12 12:24:36.826 16587-16592/com.ebookfrenzy.fragmentexample I/zygote64: After code cache collection, code=29KB, data=27KB
    Increasing code cache capacity to 128KB
08-12 12:24:37.041 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: ViewPostIme pointer 0
08-12 12:24:37.115 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: ViewPostIme pointer 1
08-12 12:24:37.125 16587-16587/com.ebookfrenzy.fragmentexample D/InputMethodManager: SSI - flag : 0 Pid : 16587 view : com.ebookfrenzy.fragmentexample
08-12 12:24:37.203 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: MSG_RESIZED: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 84 - 0, 168) vi=Rect(0, 84 - 0, 1253) or=1
08-12 12:24:38.620 16587-16592/com.ebookfrenzy.fragmentexample I/zygote64: Do partial code cache collection, code=60KB, data=48KB
    After code cache collection, code=60KB, data=48KB
    Increasing code cache capacity to 256KB
08-12 12:24:41.591 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: ViewPostIme pointer 0
08-12 12:24:41.675 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: ViewPostIme pointer 1
08-12 12:24:43.420 16587-16592/com.ebookfrenzy.fragmentexample I/zygote64: Compiler allocated 8MB to compile void android.view.ViewRootImpl.performTraversals()
08-12 12:24:43.791 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: ViewPostIme pointer 0
08-12 12:24:44.227 16587-16592/com.ebookfrenzy.fragmentexample I/zygote64: Do full code cache collection, code=122KB, data=100KB
    After code cache collection, code=105KB, data=70KB
08-12 12:24:44.261 16587-16587/com.ebookfrenzy.fragmentexample D/ViewRootImpl@9c84bd9[FragmentExampleActivity]: ViewPostIme pointer 1
08-12 12:24:44.263 16587-16587/com.ebookfrenzy.fragmentexample E/InputEventReceiver: Exception dispatching input event.
08-12 12:24:44.263 16587-16587/com.ebookfrenzy.fragmentexample E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
08-12 12:24:44.282 16587-16587/com.ebookfrenzy.fragmentexample E/MessageQueue-JNI: kotlin.NotImplementedError: An operation is not implemented: not implemented
        at com.ebookfrenzy.fragmentexample.ToolbarFragment.onStopTrackingTouch(ToolbarFragment.kt:61)
        at android.widget.SeekBar.onStopTrackingTouch(SeekBar.java:123)
        at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:1289)
        at android.view.View.dispatchTouchEvent(View.java:12539)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:600)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3384)
        at com.android.tools.profiler.support.event.WindowProfilerCallback.dispatchTouchEvent(WindowProfilerCallback.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:562)
        at android.view.View.dispatchPointerEvent(View.java:12787)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5625)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5420)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4966)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4932)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5069)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4940)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5126)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4966)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4932)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4940)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7672)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7612)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7573)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7783)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:197)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:325)
        at android.os.Looper.loop(Looper.java:142)
        at android.app.ActivityThread.main(ActivityThread.java:6940)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
08-12 12:24:44.282 16587-16587/com.ebookfrenzy.fragmentexample D/AndroidRuntime: Shutting down VM
08-12 12:24:44.289 16587-16587/com.ebookfrenzy.fragmentexample E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ebookfrenzy.fragmentexample, PID: 16587
    kotlin.NotImplementedError: An operation is not implemented: not implemented
        at com.ebookfrenzy.fragmentexample.ToolbarFragment.onStopTrackingTouch(ToolbarFragment.kt:61)
        at android.widget.SeekBar.onStopTrackingTouch(SeekBar.java:123)
        at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:1289)
        at android.view.View.dispatchTouchEvent(View.java:12539)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:600)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3384)
        at com.android.tools.profiler.support.event.WindowProfilerCallback.dispatchTouchEvent(WindowProfilerCallback.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:562)
        at android.view.View.dispatchPointerEvent(View.java:12787)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5625)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5420)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4966)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4932)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5069)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4940)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5126)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4966)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4932)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4940)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7672)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7612)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7573)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7783)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:197)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:325)
        at android.os.Looper.loop(Looper.java:142)
        at android.app.ActivityThread.main(ActivityThread.java:6940)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

只需实现所有 3 个方法:

override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
    seekvalue = progress
}

override fun onStartTrackingTouch(arg0: SeekBar) {
    seekvalue = arg0.progress
}

override fun onStopTrackingTouch(arg0: SeekBar) {
    seekvalue = arg0.progress
}


在最后 2 个方法的 header 中将 arg0: SeekBar 替换为 arg0: SeekBar?