user4744059
user4744059

Reputation:

FIXED Unable to find explicit activity class annoying error

I did some research one this Exception but none of these answers here helped me. So what I need to do is when I click a list item, it moves to another activity but I end up getting the same annoying Exception. It tells to look into Android Manifest file but everything is alright there!

Here is my code

Intent:

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    String strItemTextView = mForecastAdapter.getItem(position);
    Intent intent = new Intent(getActivity(), DetailActivity.class);
    intent.putExtra(Intent.EXTRA_TEXT, strItemTextView);
    startActivity(intent);
}

And Android Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.sunshine2">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.example.android.sunshine2.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.android.sunshine2.DetailActivity"
            android:label="@string/title_activity_detail"
            android:parentActivityName="com.example.android.sunshine2.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.android.sunshine2.MainActivity" />
        </activity>
    </application>

</manifest>

UPDATE:

Here is my code for DetailActivity:

package com.example.android.sunshine2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class DetailActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
            return rootView;
        }
    }
}

I get an exception like this:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.android.sunshine2/com.example.android.sunshine2.DetailActivity}; have you declared this activity in your AndroidManifest.xml?

And it points to this line of the code:

intent.putExtra(Intent.EXTRA_TEXT, strItemTextView);

UPDATE #2:

Now things are getting really weird. If I use relative path for activity name BUT leave a full path for a Parent activity name (Code snippet of manifest file):

<activity
        android:name=".DetailActivity" // <!--Activity I wanna go to (relative path)-->
        android:label="@string/title_activity_detail"
        android:parentActivityName="com.example.android.sunshine2.MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.sunshine2.MainActivity" /><!--Full path-->
    </activity>

I get the same Exception but this time it points to this line of code:

startActivity(intent);

UPDATE #3: Please take a look at the comment I managed to fix my problem

Upvotes: 2

Views: 2236

Answers (3)

user4744059
user4744059

Reputation:

So I managed to fix my problem. Since I am just learning on Udacity, after 2days of no answers I decided to look into the their solution. The problem is that their tutorials are about 1 year old and since then Android Studio was updated quite a lot. When you are creating a new project with fragment on the latest version of Android studio, for a layout you get three files:

activity_main.xml
content_main.xml
fragment_main.xml

As in previous versions of Android Studio, you wold only get two files:

activity_main.xml
fragment_main.xml

So what I did was I deleted content_main.xml and voilà. It worked!

That was really weird and I am marking this question as answered and I hope it will help for other users as well!

Upvotes: 0

Zucch
Zucch

Reputation: 359

You already have a declared package in your manifest:

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

So as far as I remember you should just declare your activity in manifest with relative path only:

<activity
    android:name=".DetailActivity"
    android:label="@string/title_activity_detail"
    android:parentActivityName=".MainActivity"
    android:theme="@style/AppTheme.NoActionBar">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.android.sunshine2.MainActivity" />
        </activity>

Upvotes: 1

Sush
Sush

Reputation: 3874

can you try below code

      Intent intent = new Intent(view.getContext(), DetailActivity.class);
        intent.putExtra(Intent.EXTRA_TEXT, strItemTextView);

Upvotes: 0

Related Questions