我是Android编程的新手,现在我正在学习如何构建一个Android闹钟应用程序,它将显示"上升和闪耀"从点击按钮时的任何时间开始20秒后。但是,每次它都会在模拟器和手机中强行关闭。弹出窗口继续显示"应用程序alarmmanager2(进程com.example.alarmmanager2)意外停止。请再试一次。"
这是源代码: 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm Example" />
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"/>
</LinearLayout>
的src / java的:
package alarmmanager2;
import com.example.alarmmanager2.*;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener{
final static private long ONE_SECOND = 1000;
final static private long TWENTY_SECONDS = ONE_SECOND * 20;
PendingIntent pi;
BroadcastReceiver br;
AlarmManager am;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setup();
findViewById(R.id.the_button).setOnClickListener(this);
}
private void setup() {
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
}
};
registerReceiver(br, new IntentFilter("com.authorwjf.wakeywakey") );
pi = PendingIntent.getBroadcast( this, 0, new Intent("com.authorwjf.wakeywakey"),
0 );
am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
}
@Override
public void onClick(View v) {
am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +
TWENTY_SECONDS, pi );
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmmanager2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="alarmmanager.AlarmManagerBroadcastReceiver">
</receiver>
</application>
</manifest>
编辑: 在我将类更改为com.example.alarmmanager2之后,伙计们感谢您借助我的帮助,但同样的问题仍在发生,以下是我从logcat复制的日志
05-20 07:18:55.302: D/AndroidRuntime(324): Shutting down VM
05-20 07:18:55.313: W/dalvikvm(324): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-20 07:18:55.422: E/AndroidRuntime(324): FATAL EXCEPTION: main
05-20 07:18:55.422: E/AndroidRuntime(324): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.alarmmanager2/com.example.alarmmanager2.Main}: java.lang.ClassNotFoundException: com.example.alarmmanager2.Main in loader dalvik.system.PathClassLoader[/data/app/com.example.alarmmanager2-1.apk]
05-20 07:18:55.422: E/AndroidRuntime(324): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-20 07:18:55.422: E/AndroidRuntime(324): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-20 07:18:55.422: E/AndroidRuntime(324): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-20 07:18:55.422: E/AndroidRuntime(324): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-20 07:18:55.422: E/AndroidRuntime(324): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 07:18:55.422: E/AndroidRuntime(324): at android.os.Looper.loop(Looper.java:123)
05-20 07:18:55.422: E/AndroidRuntime(324): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-20 07:18:55.422: E/AndroidRuntime(324): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 07:18:55.422: E/AndroidRuntime(324): at java.lang.reflect.Method.invoke(Method.java:521)
05-20 07:18:55.422: E/AndroidRuntime(324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-20 07:18:55.422: E/AndroidRuntime(324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-20 07:18:55.422: E/AndroidRuntime(324): at dalvik.system.NativeStart.main(Native Method)
05-20 07:18:55.422: E/AndroidRuntime(324): Caused by: java.lang.ClassNotFoundException: com.example.alarmmanager2.Main in loader dalvik.system.PathClassLoader[/data/app/com.example.alarmmanager2-1.apk]
05-20 07:18:55.422: E/AndroidRuntime(324): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-20 07:18:55.422: E/AndroidRuntime(324): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-20 07:18:55.422: E/AndroidRuntime(324): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-20 07:18:55.422: E/AndroidRuntime(324): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-20 07:18:55.422: E/AndroidRuntime(324): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-20 07:18:55.422: E/AndroidRuntime(324): ... 11 more
不要犹豫,纠正我的错误。
答案 0 :(得分:0)
根据上面的评论,我将假设(无法读取您的LogCat文件)您的应用程序因此崩溃:
registerReceiver(br, new IntentFilter("com.authorwjf.wakeywakey") );
pi = PendingIntent.getBroadcast( this, 0, new Intent("com.authorwjf.wakeywakey")
您的应用正在尝试查找com.authorwjf.wakeywakey,但因为您的程序中没有名为wakeywakey的课程,所以它无法正常工作。 除非你把它命名为com.example.wakeywakey,因为你的程序名为com.example.alarmmanager2
,它再也无法工作。如果您正在使用Eclipse,请尝试在底部看到一个选项卡调用LogCat,这将告诉您问题的确切位置。如果您可以通过编辑问题上传logcat所说的内容
答案 1 :(得分:0)
您的应用程序包是com.example.alarmmanager2
,并且在您的清单中,您已经声明了这样的启动器活动:
<activity
android:name=".Main" ..>
....
</activity>
因此Android运行时将尝试在应用程序启动时加载类com.example.alarmmanager2.Main
。但是您的Main
类位于包alarmmanager2
中,因此无法找到类com.example.alarmmanager2.Main
。
所以你有两个选择:
Main
课程移至包com.example.alarmmanager2
android:name="alarmmanager2.Main"