简体   繁体   English

开发应用程序每次都被强制关闭

[英]Developing app is forced to close every time

I am new to android programming and now I'm learning how to build an Android alarm clock application where it will display "rise and shine" after 20 seconds from whatever the time happened to be when the button was clicked. 我是android编程的新手,现在我正在学习如何构建一个Android闹钟应用程序,无论单击该按钮的时间是20秒钟,它都会显示“升起和升起”。 However, each time it would force close in both emulator and my phone. 但是,每次它都会强制关闭模拟器和我的手机。 And the popped-out window keep displaying "The application alarmmanager2 (process com.example.alarmmanager2) has stopped unexpectedly. Pls try again." 并且弹出的窗口继续显示“应用程序alarmmanager2(进程com.example.alarmmanager2)已意外停止。请重试。”

And this is the source code: layout: 这是源代码:layout:

<?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: 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 );
           }
    }

manifest: 表现:

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

EDIT : guys thank you for lending me your helping hand, after i change the class to com.example.alarmmanager2, but same problem still happening and the following is the log i copy from the logcat 编辑:伙计们,谢谢您在我将类更改为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

Do not hesitate to correct my mistakes. 不要犹豫,纠正我的错误。

As per the comments above, I am going to assume (without being able to read your LogCat file) that your application is crashing because of this: 根据上面的评论,我将假定(无法读取您的LogCat文件)您的应用程序因此而崩溃:

 registerReceiver(br, new IntentFilter("com.authorwjf.wakeywakey") );
                      pi = PendingIntent.getBroadcast( this, 0, new Intent("com.authorwjf.wakeywakey")

Your app is trying to find com.authorwjf.wakeywakey but because there isn't a class in your program called wakeywakey it won't work. 您的应用正在尝试查找com.authorwjf.wakeywakey,但是由于程序中没有名为wakeywakey的类,因此无法正常工作。 Again it wouldn't work unless you called it com.example.wakeywakey as your program is called com.example.alarmmanager2 同样,除非您将其命名为com.example.wakeywakey,否则它将无法正常工作,因为您的程序名为com.example.alarmmanager2

If you are using Eclipse, try and see at the bottom there is a tab call LogCat, this will tell you exactly where the issue is. 如果您使用的是Eclipse,请尝试在底部看到一个名为LogCat的选项卡,它会告诉您问题的确切位置。 If you can upload what the logcat says by editing your question 如果您可以通过编辑问题来上传日志猫说的话

Your application package is com.example.alarmmanager2 , and in your manifest you have declared the launcher activity like this: 您的应用程序包是com.example.alarmmanager2 ,并且在清单中已声明启动器活动,如下所示:

<activity
    android:name=".Main" ..>
    ....
</activity>

So the Android runtime will try to load the class com.example.alarmmanager2.Main when your application starts. 因此,Android运行时将在应用程序启动时尝试加载com.example.alarmmanager2.Main类。 But your Main class is in the package alarmmanager2 , so it fails to find the class com.example.alarmmanager2.Main . 但是您的Main类在包alarmmanager2 ,因此找不到com.example.alarmmanager2.Main类。

So you have two options: 因此,您有两种选择:

  1. Move the Main class to package com.example.alarmmanager2 Main类移动到包com.example.alarmmanager2
  2. Precede your activity name with the package name in the manifest file: android:name="alarmmanager2.Main" 在清单文件中的软件包名称之前添加您的活动名称: android:name="alarmmanager2.Main"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM