javaandroidandroid-fragmentsandroid-debugging

Why am I getting the error "getParentActivityIntent : badActivity name" after committing my Fragment?


I am creating a simple FragmentList App, but for some reason I keep getting the error:

Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
                    "' in manifest");

In my debugger in Activity.java

This happens in my MainActivity.java file right after .commit():

  public class MainActivity extends AppCompatActivity {    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();

        Fragment fragment = fm.findFragmentById(R.id.myContainer);

        if(fragment == null){
            fragment = new MyFragment();
            fm.beginTransaction()
                    .add(R.id.myContainer, fragment)
                    .commit();
        }
    }
}

MyFragment.java file:

public class MyFragment extends ListFragment {
    List<Course> courses = new CourseData().courseArrayList();

    public MyFragment(){

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(), R.layout.courselist_item, courses);

        setListAdapter(arrayAdapter);
 
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.courselist_item, container, false);
        return view;
    }
}

ArrayAdapter.java:

public class ArrayAdapter extends android.widget.ArrayAdapter<Course> {
    private Context context;
    private List<Course> courses;

    public ArrayAdapter(@NonNull Context context, int resource, List<Course> courses) {
        super(context, resource, courses);
        this.context = context;
        this.courses = courses;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Course course = courses.get(position);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.courselist_item, null);


        ImageView imageView = view.findViewById(R.id.course_image_id);
        imageView.setImageResource(course.getImageResourceId(context));

        TextView textView = view.findViewById(R.id.course_name);
        textView.setText(course.getCourseName());

        return view;

    }
}

Course.java:

package com.krish.mylistfragmennt;

import android.content.Context;

public class Course {
    private String courseName;
    private String courseImage;

    public Course(String courseName, String courseImage) {
        this.courseName = courseName;
        this.courseImage = courseImage;
    }

    public int getImageResourceId(Context context){
        return context.getResources().getIdentifier(this.courseImage, "drawable", context.getPackageName());

    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseImage() {
        return courseImage;
    }

    public void setCourseImage(String courseImage) {
        this.courseImage = courseImage;
    }
}

CourseData.java:

public class CourseData {

    public String[] courseNames = {"First Course", "Second Course", "Third Course"};

    public ArrayList<Course> courseArrayList(){
        ArrayList<Course> list = new ArrayList<>();

        for(int i = 0; i< courseNames.length; i++){
            Course course = new Course(courseNames[i], courseNames[i].replace(" ", "").toLowerCase());
            list.add(course);

        }
        return list;
    }

}

Manifest file:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyListFragmennt">
        <activity android:name=".MainActivity"
            android:parentActivityName=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Solution

  • I think the problem is in instantiating the adapter within onCreate() callback which is earlier than the fragment can access its activity with getActivity().

    So, try to transfer that to onCreateView() callback.

    Also use requireActivity() as it's safer than getActivity()

    public class MyFragment extends ListFragment {
        List<Course> courses = new CourseData().courseArrayList();
    
        public MyFragment(){
    
        }
    
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.courselist_item, container, false);
    
            ArrayAdapter arrayAdapter = new ArrayAdapter(requireActivity(), R.layout.courselist_item, courses);
    
            setListAdapter(arrayAdapter);   
            return view;
        }
    }