Search code examples
androidasynctaskloader

Error while using AsyncTaskLoader


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?


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.