我有2个项目(用于测试活动的调用) PRO1
和 PRO2
。
com.example.pro1
和 com.example.pro2
是其各自的包。
Pro2的是库,由PRO1。引用
所有活动都列在这两个清单文件。
我打电话PRO2,从PRO1。
我不知道我要去的地方错了。
pro'1s
的布局是这样的:
PRO2的
布局(我之后按钮preSS应该得到):
这就是我以后按钮preSS获得:
PRO1的
MainActivity:
公共类MainActivity延伸活动{
意向意图= NULL;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
意图=新的意图(MainActivity.this,com.example.pro2.Pro2.class);
按钮B =(按钮)findViewById(R.id.button1);
b.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v){
MainActivity.this.startActivity(意向);
}});
}
Pro2的公司
活动:
公共类Pro2的扩展android.app.Activity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
意向意图= getIntent();
的setContentView(R.layout.activity_pro2);
}
}
PRO1的
清单:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.pro1
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =18/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.pro1.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=com.example.pro2.Pro2
机器人:标签=@字符串/ APP_NAME
机器人:parentActivityName =com.example.pro1.MainActivity
>
< /活性GT;
< /用途>
< /舱单>
Pro2的公司
清单:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.pro2
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =18/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.pro2.Pro2
机器人:标签=@字符串/ APP_NAME
机器人:parentActivityName =com.example.pro1.MainActivity
>
&所述;元数据
机器人:名称=android.support.PARENT_ACTIVITY
机器人:值=com.example.pro1.MainActivity/>
< /活性GT;
< /用途>
< /舱单>
日志:
十二月2日至25号:38:31.989:E / AndroidRuntime(1187):致命异常:主要
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):android.content.ActivityNotFoundException:无法找到明确的活动类{com.example.pro1 / com.example.pro2.pro2};有你宣布你的AndroidManifest.xml这个活动?
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.app.Activity.startActivityForResult(Activity.java:3390)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.app.Activity.startActivityForResult(Activity.java:3351)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.app.Activity.startActivity(Activity.java:3587)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.app.Activity.startActivity(Activity.java:3555)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在com.example.pro1.MainActivity $ 1.onClick(MainActivity.java:24)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.view.View.performClick(View.java:4240)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.view.View $ PerformClick.run(View.java:17721)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.os.Handler.handleCallback(Handler.java:730)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.os.Handler.dispatchMessage(Handler.java:92)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.os.Looper.loop(Looper.java:137)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在android.app.ActivityThread.main(ActivityThread.java:5103)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在java.lang.reflect.Method.invokeNative(本机方法)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在java.lang.reflect.Method.invoke(Method.java:525)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
十二月2号至25号:38:31.989:E / AndroidRuntime(1187):在dalvik.system.NativeStart.main(本机方法)
十二月2号至25号:38:36.060:I /过程(1187):发送信号。 PID:1187 SIG:9
更新,文件:
图书馆活动:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.libraryactivity
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =18/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ app_n
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.libraryactivity.LibraryActivity
机器人:出口=真
机器人:标签=@字符串/ app_n>
< /活性GT;
< /用途>
< /舱单>
主要活动:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.mainactivity
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =18/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.mainactivity.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名称=com.example.libraryactivity.LibraryActivity>
< /活性GT;
< /用途>
< /舱单>
解决方案
您MainActivity code这样的..
公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮B =(按钮)findViewById(R.id.button1);
b.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
意图I =新的意图(MainActivity.this,
com.example.project2.LibraryActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ⅰ);
}
});
}
}
和你LibraryActivity喜欢..
公共类LibraryActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity2_main);
}
}
主要项目清单是如..
这里是重要的thing..you需要声明的库项目活动
在你的主项目
<使用-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =17/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.project1.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名称=com.example.project2.LibraryActivity>
< /活性GT;
< /用途>
库项目的清单是如..
<使用-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =17/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.project2.LibraryActivity
机器人:出口=真
机器人:标签=@字符串/ APP_NAME>
< /活性GT;
< /用途>
I have 2 projects (for testing the calling of activity) pro1
and pro2
.
com.example.pro1
and com.example.pro2
are their respective packages.
Pro2 is a library, referenced by pro1.
All the Activities are listed in both manifest files.
I am calling pro2, from pro1.
I dont know where I am going wrong.
pro'1s
layout looks like:
pro2's
Layout(what I should get after button press):
And this is what I get after button press:
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
activity:
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
manifest :
<?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
Manifest:
<?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>
The LOGS:
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
UPDATE, the files:
library activity:
<?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>
Main activity:
<?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>
解决方案
Your MainActivity code like this..
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);
}
});
}
}
And you LibraryActivity like..
public class LibraryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
}
}
Main project manifest is like..
Here is the important thing..you need to declare the Library project Activity
in your Main Project
<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>
Manifest of Library project is like..
<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>
上一篇:需要新的任务backstack单一的活动任务、backstack
下一篇:如何使一个成功的活动按钮可见frominvisible happend在另一活动按钮、frominvisible、happend