Question

This is my FragmentListArraySupport.java

    package com.test.methods;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragmentListArraySupport extends FragmentActivity {

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create the list fragment and add it as our sole content.
    if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
        final ArrayListFragment list = new ArrayListFragment();
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
    }
}

public static class ArrayListFragment extends ListFragment {

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Shakespeare.TITLES));
    }

    @Override
    public void onListItemClick(final ListView l, final View v, final int position, final long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}
}

This is my loader:

    package com.test.methods;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v4.content.AsyncTaskLoader;

public class AsyncLoader extends AsyncTaskLoader<String[]> {

    public AsyncLoader(Context context) {
        super(context);
        // TODO Auto-generated constructor stub


    }

    @Override
    public String[] loadInBackground() {
        // TODO Auto-generated method stub
        String[] S = {"hello","hi","bye"};
        return S;
    }


}

My manifest is this:

<activity android:name=".app.FragmentListArraySupport"
        android:label="@string/fragment_list_array_support">
</activity>

Error:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.crumbin.main/com.test.methods.FragmentListArraySupport}; have you declared this activity in your AndroidManifest.xml?

What might be the error?

This is the fast time I'm dealing with fragments and also using AsyncTaskLoaders which hasn't got much documentation or examples. What my code does or should do is just print a list of strings. I can them improve on it.

Is there something wrong with my code? Where?

Was it helpful?

Solution

Your manifest is completely.

Wrong:

<activity android:name=".app.FragmentListArraySupport"
        android:label="@string/fragment_list_array_support">
</activity>

Correct: Let us assume that your activity is in the package com.test.ActivityName

Your manifest should have this to include the activity:

<activity android:name="com.test.ActivityName" android:label="Activity Name" > </activity>

Or For the second way to work, go to the Strings.xml file and add Activity_name = "Any label for the activity."

That is all.

OTHER TIPS

A fragment must be contained in an activity (you can do this in XML or dynamically). In the manifest you have to specify the activities, not the fragments, which may be contained.

Your code in the manifest file is wrong. There should be an activity instead of a fragment. That is why you get that CAST exception.

Please check out the samples: http://developer.android.com/resources/samples/Support4Demos/index.html

You can download them with the Android SDK Manager.

I attach a simple example below:

public class FragmentListArraySupport extends FragmentActivity {

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create the list fragment and add it as our sole content.
    if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
        final ArrayListFragment list = new ArrayListFragment();
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
    }
}

public static class ArrayListFragment extends ListFragment {

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Shakespeare.TITLES));
    }

    @Override
    public void onListItemClick(final ListView l, final View v, final int position, final long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}
}

and in you manifest file:

    <activity android:name=".app.FragmentListArraySupport"
            android:label="@string/fragment_list_array_support">
    </activity>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top