我有2个项目(用于测试活动的调用)pro1
和pro2
。com.example.pro1
和com.example.pro2
是它们各自的软件包。
Pro2是由pro1引用的库。
所有活动都列在两个清单文件中。
我正在从pro1呼叫pro2。
我不知道我要去哪里错了。pro'1s
布局看起来像:pro2's
布局(按下按钮后我应该得到什么):
这是按下按钮后我得到的:pro1's
MainActivity:
public class MainActivity extends Activity {
Intent intent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(MainActivity.this,com.example.pro2.Pro2.class);
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
MainActivity.this.startActivity(intent);
} });
}
Pro2's
活动:public class Pro2 extends android.app.Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_pro2);
}
}
pro1's
清单:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pro1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pro1.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>
<activity
android:name="com.example.pro2.Pro2"
android:label="@string/app_name"
android:parentActivityName="com.example.pro1.MainActivity"
>
</activity>
</application>
</manifest>
Pro2's
清单:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pro2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pro2.Pro2"
android:label="@string/app_name"
android:parentActivityName="com.example.pro1.MainActivity"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.pro1.MainActivity" />
</activity>
</application>
</manifest>
日志:
02-25 12:38:31.989: E/AndroidRuntime(1187): FATAL EXCEPTION: main
02-25 12:38:31.989: E/AndroidRuntime(1187): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.pro1/com.example.pro2.pro2}; have you declared this activity in your AndroidManifest.xml?
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.app.Activity.startActivityForResult(Activity.java:3390)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.app.Activity.startActivityForResult(Activity.java:3351)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.app.Activity.startActivity(Activity.java:3587)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.app.Activity.startActivity(Activity.java:3555)
02-25 12:38:31.989: E/AndroidRuntime(1187): at com.example.pro1.MainActivity$1.onClick(MainActivity.java:24)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.view.View.performClick(View.java:4240)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.view.View$PerformClick.run(View.java:17721)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.os.Handler.handleCallback(Handler.java:730)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.os.Looper.loop(Looper.java:137)
02-25 12:38:31.989: E/AndroidRuntime(1187): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-25 12:38:31.989: E/AndroidRuntime(1187): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 12:38:31.989: E/AndroidRuntime(1187): at java.lang.reflect.Method.invoke(Method.java:525)
02-25 12:38:31.989: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-25 12:38:31.989: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-25 12:38:31.989: E/AndroidRuntime(1187): at dalvik.system.NativeStart.main(Native Method)
02-25 12:38:36.060: I/Process(1187): Sending signal. PID: 1187 SIG: 9
更新,文件:
图书馆活动:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.libraryactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_n"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.libraryactivity.LibraryActivity"
android:exported="true"
android:label="@string/app_n" >
</activity>
</application>
</manifest>
主要活动:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mainactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mainactivity.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>
<activity android:name="com.example.libraryactivity.LibraryActivity" >
</activity>
</application>
</manifest>
最佳答案
您的MainActivity代码是这样的。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,
com.example.project2.LibraryActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
});
}
}
和你的LibraryActivity喜欢..
public class LibraryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
}
}
主项目清单就像..
这是重要的事情..您需要在主项目中声明Library项目
Activity
<uses-sdk
android:minSdkVersion="8"
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.project1.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>
<activity android:name="com.example.project2.LibraryActivity" >
</activity>
</application>
图书馆项目清单是..
<uses-sdk
android:minSdkVersion="8"
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.project2.LibraryActivity"
android:exported="true"
android:label="@string/app_name" >
</activity>
</application>