4

我正在编写一个代码,其中我允许用户使用 Intent 将事件添加到日历,但是每当我尝试将事件插入较低版本时,得到:不幸的是应用程序已停止并且错误是:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.EDIT typ=vnd.android.cursor.item/event (has extras) }

AndroidManifest.xml:-

 <uses-permission android:name="android.permission.READ_CALENDAR"> </uses-permission>
 <uses-permission android:name="android.permission.WRITE_CALENDAR"> </uses-permission>

Java代码:-

final Button button = (Button) findViewById(R.id.button1);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) 
             {
                 if (Build.VERSION.SDK_INT >= 14) {

                     Intent intent = new Intent(Intent.ACTION_INSERT).setData(Events.CONTENT_URI)
                         .putExtra(Events.TITLE, "Yoga")
                         .putExtra(Events.DESCRIPTION, "Group class")
                         .putExtra(Events.EVENT_LOCATION, "The Gym");
                      startActivity(intent);

                  } else {
                      Intent intent = new Intent(Intent.ACTION_EDIT);
                      intent.setType("vnd.android.cursor.item/event");                      
                      intent.putExtra(Events.TITLE, "Yoga");
                      intent.putExtra(Events.DESCRIPTION, "Group class");
                      intent.putExtra(Events.EVENT_LOCATION, "The Gym");
                      startActivity(intent);
                  }
             }
         });
    }

清单.xml:-

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.event"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission
  android:name="android.permission.READ_CALENDAR">
  </uses-permission>
<uses-permission
   android:name="android.permission.WRITE_CALENDAR">
   </uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.event.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

日志猫:

11-04 16:34:26.902: E/AndroidRuntime(370): FATAL EXCEPTION: main
11-04 16:34:26.902: E/AndroidRuntime(370): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.EDIT typ=vnd.android.cursor.item/event (has extras) }
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.app.Activity.startActivityForResult(Activity.java:2817)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.app.Activity.startActivity(Activity.java:2923)
11-04 16:34:26.902: E/AndroidRuntime(370):  at com.example.event.MainActivity$1.onClick(MainActivity.java:39)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.view.View.performClick(View.java:2408)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.view.View$PerformClick.run(View.java:8816)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.os.Handler.handleCallback(Handler.java:587)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.os.Looper.loop(Looper.java:123)
11-04 16:34:26.902: E/AndroidRuntime(370):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 16:34:26.902: E/AndroidRuntime(370):  at java.lang.reflect.Method.invokeNative(Native Method)
11-04 16:34:26.902: E/AndroidRuntime(370):  at java.lang.reflect.Method.invoke(Method.java:521)
11-04 16:34:26.902: E/AndroidRuntime(370):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-04 16:34:26.902: E/AndroidRuntime(370):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-04 16:34:26.902: E/AndroidRuntime(370):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

如果您在模拟器中运行应用程序,这将不起作用,您必须在物理设备中运行您的应用程序。

于 2013-11-04T11:25:44.103 回答