【问题标题】:Android App is not working on device whereas its working fine on emulatorAndroid 应用程序无法在设备上运行,而在模拟器上运行良好
【发布时间】:2015-06-21 16:51:11
【问题描述】:

我有一个在模拟器上运行良好的 android 应用程序。但是当我在设备上尝试它时,它显示了一些问题,例如“无法执行活动的方法”。以下是 logcat 报告。

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3640)
        at android.view.View.performClick(View.java:4249)
        at android.view.View$PerformClick.run(View.java:17764)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
Caused by: java.lang.NoSuchMethodError: android.app.AlarmManager.setExact
        at com.example.manju.remainder.service.task.AlarmTask.run(AlarmTask.java:49)
        at com.example.manju.remainder.service.ScheduleService.setAlarm(ScheduleService.java:50)
        at com.example.manju.remainder.service.ScheduleClient.setAlarmForNotification(ScheduleClient.java:55)
        at com.example.manju.remainder.MainActivity.callAlarm(MainActivity.java:507)
        at com.example.manju.remainder.MainActivity.checkDates(MainActivity.java:474)
        at com.example.manju.remainder.MainActivity.submitButtonClicked(MainActivity.java:160)

任何人都可以帮助我解决这个问题..

【问题讨论】:

  • 你确定模拟器和你的测试设备是同一个版本吗?
  • 当然,它在模拟器上运行正常。
  • 抱歉如何检查!!
  • 那是你的问题。 [setExact()](developer.android.com/reference/android/app/…, long, android.app.PendingIntent)) 要求最低 API 19 和 you have 18
  • 谢谢你..!特别是@codeMagic。它现在正在工作..!

标签: android android-studio


【解决方案1】:

试试这个:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
   // only for kitkat and newer versions
   android.app.AlarmManager.setExact(int type, long triggerAtMillis, PendingIntent operation);
} else {
   android.app.AlarmManager.set(int type, long triggerAtMillis, PendingIntent operation);
}

基本上,.setExact(...); 方法需要 API 19 及更高版本(这只是绕过 android 提供的优化的一种方法,以最大限度地延长电池寿命)。

【讨论】:

  • @ManjuJK 很高兴它起作用了:D,接受它作为正确答案,这样其他人也可能受益
【解决方案2】:

另一种解决方案是,将 build.gradle 中的 minSdkVersion 设置为手机中的那个。

假设你的手机版本是 Android 5.0 ,那么 API Level 是 21。你可以设置 minSdkVersion 21。

defaultConfig {
    minSdkVersion 21
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}

这绝对有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多