如何在实例中传递字符串数组?Android Fragment传参报错解决
调用Fragment的newInstance方法时提示“CANNOT RESOLVE SYMBOL NAME/FIRSTNAME”错误的修复方案
问题描述
我开发了一个从MainActivity调用的PersonFragment,Fragment代码本身无报错,但在MainActivity中调用PersonFragment的newInstance方法传递字符串数组时,提示“CANNOT RESOLVE SYMBOL NAME”和“CANNOT RESOLVE SYMBOL FIRSTNAME”错误。
Fragment代码
public class PersonFragment extends Fragment { private static final String ARG_NAME = "name"; private static final String ARG_FIRSTNAME = "firstname"; ListView lst; String[] name = {"Dockar","Nilson"}; String[] firstname = {"Jack","Michel"}; public static PersonFragment newInstance(String[] name, String[] firstname) { PersonFragment fragment = new PersonFragment(); Bundle args = new Bundle(); args.putStringArray(ARG_NAME, name); args.putStringArray(ARG_FIRSTNAME, firstname); fragment.setArguments(args); return fragment; } Activity mainActivity; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.person_fragment, container, false); lst = view.findViewById(R.id.listviewperson); CustomeListviewAdapterTest customeListview = new CustomeListviewAdapterTest(this.mainActivity,name,firtname); lst.setAdapter(customeListview); lst.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { if(position == 0) { Intent intent = new Intent(getContext(),Jack.class); startActivity(intent); }if(position == 1) { Intent intent = new Intent(getContext(),Michel.class); startActivity(intent); } } }); if (getArguments() != null) { name = getArguments().getStringArray(ARG_NAME); firstname = getArguments().getStringArray(ARG_FIRSTNAME); } return view; } }
MainActivity调用代码
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.drawer_person: PersonFragment fragment = PersonFragment.newInstance(HAVE ERROR HERE CANNOT RESOLVE SYMBOL NAME, CANNOT RESOLVE SYMBOL FIRSTNAME); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).addToBackStack(null).commit(); break; }
修复方案
1. 修复MainActivity的参数传递错误
错误原因是你在调用newInstance时,直接写了NAME和FIRSTNAME,但这两个是PersonFragment内部的私有常量,MainActivity无法访问。你需要传入实际的字符串数组作为参数,示例如下:
方式一:先定义数组再传递
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.drawer_person: // 定义要传递的字符串数组 String[] names = {"Dockar","Nilson"}; String[] firstNames = {"Jack","Michel"}; // 传入数组到newInstance PersonFragment fragment = PersonFragment.newInstance(names, firstNames); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).addToBackStack(null).commit(); break; } // 其他代码... }
方式二:直接传递字面量数组
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.drawer_person: PersonFragment fragment = PersonFragment.newInstance( new String[]{"Dockar","Nilson"}, new String[]{"Jack","Michel"} ); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).addToBackStack(null).commit(); break; } // 其他代码... }
2. 修复Fragment内部的潜在错误
- 拼写错误:
onCreateView中初始化Adapter时,firtname是拼写错误,应该改为firstname。 - 空指针风险:
mainActivity变量未初始化,建议用getActivity()代替,确保获取到有效的上下文。 - 数据顺序问题:原代码中先初始化Adapter再从Arguments取数据,会导致Adapter使用默认的数组值,应该先取Arguments数据再初始化Adapter。
修改后的onCreateView代码:
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.person_fragment, container, false); // 优先从Arguments中获取传递的数据 if (getArguments() != null) { name = getArguments().getStringArray(ARG_NAME); firstname = getArguments().getStringArray(ARG_FIRSTNAME); } lst = view.findViewById(R.id.listviewperson); // 修复拼写错误,用getActivity()获取上下文 CustomeListviewAdapterTest customeListview = new CustomeListviewAdapterTest(getActivity(), name, firstname); lst.setAdapter(customeListview); lst.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { if(position == 0) { Intent intent = new Intent(getContext(),Jack.class); startActivity(intent); } else if(position == 1) { // 用else if优化逻辑 Intent intent = new Intent(getContext(),Michel.class); startActivity(intent); } } }); return view; }
内容的提问来源于stack exchange,提问作者Antonely93
相关产品推荐
相关产品推荐

