Anarantt
Anarantt

Reputation: 289

Cannot start activity using explicit intent

I have Starter ListActivity which starts two others: Multitouch and Accelerometer.
When the first one is touched in ListActivity it is launched very well.
The second one, however, isn`t laucnched.
Logcat reports:

06-09 22:42:55.293  12227-12227/com.mainpackage.api E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.content.ActivityNotFoundException: Unable to find explicit activity class 
{com.mainpackage.api/com.mainpackage.api.Accelerometer}; 
have you declared this activity in your AndroidManifest.xml?

I have read related questions but they didn't help me out.
Starter activity:

public class Starter extends ListActivity {

    private String[] tests = { "MultiTouch", "Accelerometer" };

    @Override
    public void onCreate(Bundle saved) {
        super.onCreate(saved);
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,tests);
        this.setListAdapter(adapter);

    }

    @Override
    public void onListItemClick(ListView list, View view, int position, long id) {
        super.onListItemClick(list,view,position,id);
        String test = tests[position];
        try {
            Class clazz = Class.forName("com.mainpackage.api."+test);
            Intent i = new Intent(this,clazz);
            startActivity(i);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mainpackage.api">

    <application android:allowBackup="true" android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
        <activity
            android:name=".Starter"
            android:label="@string/app_name"
            >
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MultiTouch"
            android:label="MultiTouch" />
        <activtiy
            android:name=".Accelerometer"
            android:label="Accelerometer" />

    </application>

</manifest>

MultiTouch and Accelerometer have no bugs. I have tried them separate.
Any ideas ?

Upvotes: 0

Views: 124

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

First, do not use Class.forName(). Either use if or a Class[] and just reference the Accelerometer.class and MultiTouch.class objects.

Second, your manifest has:

<activtiy
        android:name=".Accelerometer"
        android:label="Accelerometer" />

which has the element name spelled incorrectly. Try:

<activity
        android:name=".Accelerometer"
        android:label="Accelerometer" />

Upvotes: 2

Related Questions