1

我正在创建一个简单的FragmentList应用程序,但由于某种原因,我不断收到错误消息:

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

在我的调试器中Activity.java

这发生在我的MainActivity.java文件中.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 文件

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;

    }
}

课程.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;
    }
}

课程数据.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;
    }

}

清单文件:

<?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>
4

1 回答 1

0

我认为问题在于在onCreate()回调中实例化适配器,该回调早于片段可以使用getActivity().

因此,尝试将其转移到onCreateView()回调。

也可以使用requireActivity(),因为它比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;
    }
}
于 2021-04-23T22:49:48.933 回答