【问题标题】:Android Studio “Button Tapped!”Android Studio“点击按钮!”
【发布时间】:2015-11-06 18:01:11
【问题描述】:

感谢您阅读此问题。

我是学习为 Android 开发应用程序的新手,编码经验几乎为零。我正在学习的课程的讲师使用以下代码将一个按钮添加到我们的 Android Studio 项目中,发送消息“Button Tapped!”每次单击特定按钮时记录特定按钮单击的日志:

public void clickfunction(View view){Log.i(“Info”, “Button Tapped!”);}

我花了两天时间在运行 OS X 10.11.1 的 iMac 上安装了 Android Studio 1.4.1,并安装了最新更新以启动 Android 模拟器。

即便如此,当使用配置为 8 英寸 DROID TURBO 和 3GB RAM 1440 x 的自定义 Android 虚拟设备 (AVD) 在启动的模拟器中单击按钮时,仍会出现“Android 应用程序我的应用程序测试”停止2560. 至少日志显示以下错误,因此从技术上讲,“按钮有效”,因为它将以下消息发送到日志。它不显示,“Button Taped!”在日志中。:

安卓监视器

11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest     E/AndroidRuntime: FATAL EXCEPTION: main
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest     E/AndroidRuntime: Process: com.example.chris.myapplicationtest, PID: 3298
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime: java.lang.IllegalStateException: Could not find method   clickfunctio(View) in a parent or ancestor Context for android:onClick   attribute defined on view class android.support.v7.widget.AppCompatButton  with id 'button'
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:310)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:269)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at android.view.View.performClick(View.java:5198)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-06 11:23:39.600 3298-3298/com.example.chris.myapplicationtest E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-06 11:23:41.390 3298-3298/com.example.chris.myapplicationtest I/Process: Sending signal. PID: 3298 SIG: 9

事件日志

11:00:17 AM Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:assembleDebug]
11:00:19 AM Gradle build finished with 10 error(s) in 1s 984ms
11:09:02 AM Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:assembleDebug]
11:09:03 AM Gradle build finished with 2 error(s) in 1s 426ms
11:09:26 AM Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:assembleDebug]
11:09:40 AM Gradle build finished in 14s 275ms
11:21:02 AM Session 'app': running
11:23:38 AM IllegalStateException: no segments available. offset = 3921
11:23:38 AM Platform and Plugin Updates: The following component is ready to update: Android Auto Desktop Head Unit emulator
11:23:46 AM IllegalStateException: no segments available. offset = 3654
11:26:26 AM Can't bind to local 8700 for debugger
11:26:28 AM Can't bind to local 8700 for debugger

再次,

感谢您的宝贵时间!

感谢那些回答和阅读本文的人。您所有的答案都与我班级讨论板提供的指示完全相同。再次感谢大家!

【问题讨论】:

  • 你是否在按钮的xml中定义了onclick
  • “在父级中找不到方法 clickfunctio(View)”?你的 clickfunctio(View) 在哪里?

标签: android button logging android-studio


【解决方案1】:

onClick 方法名不匹配

因为在 Button xml 中的 android:onClick 中添加了 clickfunctio 作为方法,但在代码中方法名称为 clickfunction

【讨论】:

    【解决方案2】:

    您有一个拼写错误:在您的 XML 文件中将 clickfunctio 更改为 clickfunction

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 2011-07-14
      相关资源
      最近更新 更多