Error calling Android activity from .aar library.

2019-09-09 06:33发布

问题:

I have a project that uses googles map api that performs geofencing and other map related activities. I transformed this project into a library (.aar) so I can share it to other projects that has similar use cases with maps and geofnece.

My problem is when I create a new project and import the .aar library I cannot call activities inside the .aar library.
The app crashers after I click the button to call the activity from the library and shows an error in the debugger.

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.package.name.MapsActivity }

This is how I call the activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
    }

    //onclick button
    public void testSubmit(View view) {

        //com.package.name.MapActivity is the activity inside the .aar
        Intent intent = new Intent("com.package.name.MapsActivity");
        startActivity(intent);
    }
}

I also added the activity into my projects manifest after I imported the .aar

<activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <activity android:name="com.package.name.MapsActivity" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

回答1:

Try this:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("packagename//com.package.name, 
                                     "classname//com.package.name.MapsActivity"));
startActivity(intent);

Answer from here https://stackoverflow.com/a/9822278/4848308