gpt4 book ai didi

java - 开发app每次都强制关闭

转载 作者:行者123 更新时间:2023-11-29 08:48:51 24 4
gpt4 key购买 nike

我是 android 编程的新手,现在我正在学习如何构建一个 Android 闹钟应用程序,它会在从单击按钮时发生的任何时间起 20 秒后显示“rise and shine”。但是,每次它都会在模拟器和我的手机中强制关闭。并且弹窗一直显示“应用程序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>

源代码/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 );
}
}

list :

        <?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

请随时纠正我的错误。

最佳答案

根据上面的评论,我假设(无法读取您的 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 所说的内容

关于java - 开发app每次都强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com