Android error “unable to find explicit activity cl

2019-01-11 10:28发布

I have an android project with several packages. The structure of the packages in this case is com.WAPP.SetLocation is the package that contains the activity I want to run.

In my manifest, com.WAPP is considered the base package:

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

My activities are declared in my manifest as:

<activity android:name=".mainScreenActivity"></activity>
<activity android:name=".SetLocation.setLocationActivity"></activity>

The mainScreen activity displays fine, since it is inside the com.WAPP package. But when I try to run the setLocationActivity, I get the unable to find explicit class error. Here is how I have the intent parameters:

Intent i = new Intent();
            i.setClassName("com.WAPP.SetLocation",
                           "com.WAPP.SetLocation.setLocationActivity");
            startActivity(i);

7条回答
The star\"
2楼-- · 2019-01-11 10:56

If i'm not mistaken, the i.setClassName("com.WAPP.SetLocation","com.WAPP.SetLocation.setLocationActivity"); should be i.setClassName(getBaseContext(),"setLocationActivity"); Reference

Also try this syntax:

startActivity(new Intent(MyActivity.this, setLocationActivity.class));

and try removing the starting dot from:

<activity android:name=".SetLocation.setLocationActivity"></activity>
查看更多
淡お忘
3楼-- · 2019-01-11 10:56

in your manifest you declared it as .SetLoction.setLocationActivity but the package name is com.WAPP.SetLocation so you need to prefix that again.

Intent i = new Intent();
            i.setClassName("com.WAPP.SetLocation",
                           "com.WAPP.SetLocation.SetLocation.setLocationActivity");
            startActivity(i);
查看更多
beautiful°
4楼-- · 2019-01-11 11:02

In Xamarin Android, make sure that your Activity class has an Activity attribute

[Activity(Theme = "@style/MyTheme")]
public class MyActivity : ActivityBase
{
...
}

With that you are making sure that your activity is registered in AndroidManifest.xml

查看更多
聊天终结者
5楼-- · 2019-01-11 11:10

The first parameter is application package not the package where the activity is.

You can invoke the Activity like this.

Intent i = new Intent();
i.setClassName("com.WAPP",
               "com.WAPP.SetLocation.setLocationActivity");
startActivity(i);

It is preferred as SYLARRR suggested to have Android automatically figure that out for you. Hence the call as..

startActivity(new Intent(this, setLocationActivity.class));

It's recommended per java standards to have the package name all lower-cased and the class name as CamelCased.

查看更多
地球回转人心会变
6楼-- · 2019-01-11 11:14

In additional to the above answers make sure that your activities are declared inside application in manifest

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true">

    <activity android:name=".mainScreenActivity"></activity>
    <activity android:name=".SetLocation.setLocationActivity"></activity>

</application>
查看更多
做个烂人
7楼-- · 2019-01-11 11:19

If the new activity not in the same packet with MainActivity(you call from here?), try declare on manifest

<activity android:name="com.WAPP.SetLocation.setLocationActivity"></activity>

and in the caller

Intent intent = new Intent(this, setLocationActivity.class);
startActivity(intent);

Hope this helps!

查看更多
登录 后发表回答