我正在尝试从驻留在 android 库中的应用程序启动 Activity。当我尝试从应用程序启动活动时,我不断收到以下异常。
09-07 12:26:35.300: E/AndroidRuntime(553): FATAL EXCEPTION: main
09-07 12:26:35.300: E/AndroidRuntime(553): java.lang.IllegalStateException: Could not execute method of the activity
09-07 12:26:35.300: E/AndroidRuntime(553): at android.view.View$1.onClick(View.java:3044)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.view.View.performClick(View.java:3511)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.view.View$PerformClick.run(View.java:14105)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.os.Handler.handleCallback(Handler.java:605)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:137)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-07 12:26:35.300: E/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 12:26:35.300: E/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:511)
09-07 12:26:35.300: E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-07 12:26:35.300: E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-07 12:26:35.300: E/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method)
09-07 12:26:35.300: E/AndroidRuntime(553): Caused by: java.lang.reflect.InvocationTargetException
09-07 12:26:35.300: E/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 12:26:35.300: E/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:511)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.view.View$1.onClick(View.java:3039)
09-07 12:26:35.300: E/AndroidRuntime(553): ... 11 more
09-07 12:26:35.300: E/AndroidRuntime(553): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.smarttech.androidlibrary.LibraryActivity.LAUNCH }
09-07 12:26:35.300: E/AndroidRuntime(553): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1512)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.app.Activity.startActivityForResult(Activity.java:3190)
09-07 12:26:35.300: E/AndroidRuntime(553): at android.app.Activity.startActivity(Activity.java:3297)
09-07 12:26:35.300: E/AndroidRuntime(553): at com.smarttech.application.MainActivity.buttonLaunchActivity(MainActivity.java:25)
09-07 12:26:35.300: E/AndroidRuntime(553): ... 14 more
我在 Eclipse 中工作,我创建了两个项目——应用程序和库。
Library 通过 Properties->Android->Is Library 声明为 Android Library
应用程序通过 Properties->Android->Add 引用该库。
我遵循了使用 ADT 从 Eclipse 管理项目的说明。
图书馆:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smarttech.androidlibrary"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LibraryActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="com.smarttech.androidlibrary.LibraryActivity.LAUNCH" />
</intent-filter>
</activity>
</application>
</manifest>
应用:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smarttech.application"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.smarttech.androidlibrary.LibraryActivity">
<intent-filter>
<action android:name="com.smarttech.androidlibrary.LibraryActivity.LAUNCH"/>
</intent-filter>
</activity>
</application>
</manifest>
--
package com.smarttech.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void buttonLaunchActivity(View view) {
Intent i = new Intent("com.smarttech.androidlibrary.LibraryActivity.LAUNCH");
startActivity(i);
}
}