Dany's
Dany's

Reputation: 928

How to call an Activity in Android

I have the following scenario. I have 2 packages in my application. com.example.package1; org.otherexample.package2;

I declare in manifest like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.package1"
   android:versionCode="1"
   android:versionName="1.0" >
   <activity android:name=".ActivityfromPackage1"/>
   <activity android:name="org.otherexample.package2.ActivityFromPackage2"/>

</manifest>

This being the manifest, now I want to Call From ActivityFromPackage1 ActivityFromPackage2 I've done like this:

import org.otherexample.package2.ActivityFromPackage2
..........
Intent intent = new Intent(this,ActivityFromPackage2.class);
startActivity(intent);

I receive following error:

Unable to start Activity com.example.package1/org.otherexample.package2.ActivityFromPackage2:
JavaLang nullpointer exception

How to call the Activity? Thanks a lot.

Upvotes: 0

Views: 345

Answers (3)

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

I just checked your code. It's working in my app.

Manifest:

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

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

    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

    <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="org.pac.abcs.TestActivity" >
        </activity>
    </application>

</manifest>

Java Code:

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                startActivity(new Intent(MainActivity.this, TestActivity.class));
            }
        });

So its not issue of package declaration. Just one doubt, Did you import android.R in your second activity (ActivityFromPackage2) or com.example.package1.R? You will need to import com.example.package1.R.

Upvotes: 0

FoamyGuy
FoamyGuy

Reputation: 46856

I suspect that something outside of what you've posted here is the root of your problem. I just made an example project to test it out.

Here are my two activity declarations in manifest:

        <activity
            android:name="com.example.packagetesting.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.anotherpackage.AnotherActivity"
            android:label="@string/title_activity_another" >
        </activity>

Here is the relavent bit from MainActivity:

import com.example.anotherpackage.AnotherActivity;
...
Intent i = new Intent(this, AnotherActivity.class);
startActivity(i);

Note that in my second Activity I had to import R from the main package:

import com.example.packagetesting.R;

But after doing that everything compiles and runs correctly.

Also note in my Log file it shows:

Starting: Intent { cmp=com.example.packagetesting/com.example.anotherpackage.AnotherActivity }

Which like yours shows both of the different package names even though AnotherActivity is only in com.example.anotherpackage

Upvotes: 1

jcw
jcw

Reputation: 5162

I think that you may have to modify your manifest a little bit, try making it look more like this

   <activity android:name=".ActivityFromP2">
  <intent-filter>
    <action android:name="package2.intent.action.Launch" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

This question is useful

launch activities from different package

Upvotes: 0

Related Questions