从另一个应用程序调用活动

时间:2014-02-25 17:27:09

标签: android android-activity

我有2个项目(用于测试活动的调用)pro1pro2

com.example.pro1com.example.pro2是他们各自的套餐。

Pro2是一个库,由pro1引用。

所有活动都列在两个清单文件中。

我从pro1打电话给pro2。

我不知道我哪里出错了。

pro'1s布局如下:

enter image description here

pro2's布局(按下按钮后我应该得到的):

enter image description here

这是按下按钮后的结果:

enter image description here

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>

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,文件:

图书馆活动:

<?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>

1 个答案:

答案 0 :(得分:2)

您的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);
}
}

主要项目清单就像..

这是重要的事情..您需要在主项目中声明库项目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>