不幸的是,App已停止(Android开发)

时间:2013-02-04 10:49:44

标签: java android android-notifications

我刚刚在2天内启动了android开发,这是我无法解决的错误“不幸的是,[App]已经停止了”。

这是我打算用APP做的。

  1. 显示UI,告诉用户按下确定应用程序将开始创建日志文件。

  2. 按下OK按钮后,它将在后台运行(不需要UI)并等待来自其他应用程序的通知,并将通知中的文本写入日志文件。

  3. 这是我的AndroidManifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.notificationnotifier"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.notificationnotifier.GetNotification"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
                android:name="com.example.notificationnotifier.MonitorNotification"
                android:label="@string/app_name" android:exported="false">
    
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
        </application>
    
    </manifest>
    

    这是GetNotification.java

    public class GetNotification extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_get_notification);
    
            Button okButton = (Button) findViewById(R.id.OKButton);
            okButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent getNotification = new Intent("com.example.notificationnotifier.MonitorNotification");
                    startActivity(getNotification); 
                }
            }) ;
    
            Button cancel = (Button) findViewById(R.id.Cancel);
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    finish();
                }
            }) ;
    
        }
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
        }
    
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
        }
    
        @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
        }
    
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
        }  
    }
    

    这是MonitorNotification.java

    import android.accessibilityservice.AccessibilityService;
    import android.accessibilityservice.AccessibilityServiceInfo;
    import android.view.accessibility.AccessibilityEvent;
    
    public class MonitorNotification extends AccessibilityService{
    
        @Override
        public void onAccessibilityEvent(AccessibilityEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onInterrupt() {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        protected void onServiceConnected() {
            // TODO Auto-generated method stub
        //  super.onServiceConnected();
            AccessibilityServiceInfo info = new AccessibilityServiceInfo();
              info.feedbackType = 1;
                  info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
              info.notificationTimeout = 100; 
              setServiceInfo(info);
        }
    }
    

    这是logcat [更新]

    02-04 10:31:17.947: E/Trace(1108): error opening trace file: No such file or directory (2)
    02-04 10:31:18.648: D/gralloc_goldfish(1108): Emulator without GPU emulation detected.
    02-04 10:31:21.128: D/AndroidRuntime(1108): Shutting down VM
    02-04 10:31:21.148: W/dalvikvm(1108): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    02-04 10:31:21.169: E/AndroidRuntime(1108): FATAL EXCEPTION: main
    02-04 10:31:21.169: E/AndroidRuntime(1108): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.notificationnotifier/com.example.notificationnotifier.MonitorNotification}: java.lang.ClassCastException: com.example.notificationnotifier.MonitorNotification cannot be cast to android.app.Activity
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.os.Handler.dispatchMessage(Handler.java:99)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.os.Looper.loop(Looper.java:137)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at java.lang.reflect.Method.invoke(Method.java:511)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at dalvik.system.NativeStart.main(Native Method)
    02-04 10:31:21.169: E/AndroidRuntime(1108): Caused by: java.lang.ClassCastException: com.example.notificationnotifier.MonitorNotification cannot be cast to android.app.Activity
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
    02-04 10:31:21.169: E/AndroidRuntime(1108):     ... 11 more
    02-04 10:31:23.268: I/Process(1108): Sending signal. PID: 1108 SIG: 9
    

4 个答案:

答案 0 :(得分:1)

替换此行:

Intent getNotification = new Intent("com.example.notificationnotifier.MonitorNotification");

这一个:

Intent getNotification = new Intent(GetNotification.this,
        MonitorNotification.class);

希望它有所帮助。

答案 1 :(得分:0)

在清单文件中添加以下代码:

 <service android:name=".MyAccessibilityService"
     android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
 <intent-filter>
     <action android:name="android.accessibilityservice.AccessibilityService" />
 </intent-filter>
 . . .
</service>

答案 2 :(得分:0)

声明

可访问性被声明为AndroidManifest.xml中的任何其他服务,但它还必须指定它处理“android.accessibilityservice.AccessibilityService”意图。未能声明此意图将导致系统忽略辅助功能服务。此外,可访问性服务必须请求BIND_ACCESSIBILITY_SERVICE权限,以确保只有系统可以绑定到它。未能声明此意图将导致系统忽略辅助功能服务。以下是一个示例声明:

 <service android:name=".MyAccessibilityService"
     android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
 <intent-filter>
     <action android:name="android.accessibilityservice.AccessibilityService" />
 </intent-filter>
 . . .
</service>

答案 3 :(得分:0)

我现在知道问题是什么了,我的朋友告诉我,它应该是startService而不是getNotification.java中的startActivity

调用它的正确方法是

startService(getNotification); 

startActivity(getNotification); 

我在MonitorNotification.java的类中扩展了AccesibilityService。 :d