英文:
How to declare the ID of a view inside an ArrayList?
问题
我正在处理一个包含数百个 ID 的布局,所以我需要一种声明多个 ID 的方法。我尝试过这样做,但应用程序崩溃了。
英文:
I´m working with a layout with hundreds of ID, so I need a way of declaring several id. I tried to do that, but the app crashed.
public class middleScreenfragment extends Fragment {
private FragmentMiddleScreenfragmentBinding binding;
List<ProgressBar> progressBarList = new ArrayList<ProgressBar>();
ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentMiddleScreenfragmentBinding.inflate(inflater,container,false);
View view = binding.getRoot();
for (int i = 0; i < 10; i++)
progressBarList.add(progressBar);
progressBarList.get(0).getRootView().findViewById(R.id.id_1);
return view;
}
}
答案1
得分: 0
崩溃的原因是因为progressBar
未正确创建。您只是将空对象添加到progressBarList
中。
要解决异常,您需要为其分配一个ProgressBar
:
public class MiddleScreenfragment extends Fragment {
private FragmentMiddleScreenfragmentBinding binding;
List<ProgressBar> progressBarList = new ArrayList<ProgressBar>();
ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
// 分配进度条以使其不为空
progressBar = new ProgressBar(getContext());
...
}
}
要以编程方式分配ID,您可以使用此函数View.generateViewId()
:
生成适用于setId(int)的值。此值不会与aapt在构建时为R.id生成的ID值发生冲突。
您可以像这样使用它:
public class MiddleScreenfragment extends Fragment {
private FragmentMiddleScreenfragmentBinding binding;
List<ProgressBar> progressBarList = new ArrayList<ProgressBar>();
ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentMiddleScreenfragmentBinding.inflate(inflater, container, false);
View view = binding.getRoot();
for (int i = 0; i < 10; i++) {
progressBar = new ProgressBar(getContext());
// 以编程方式设置新创建的进度条的ID
progressBar.setId(View.generateViewId());
progressBarList.add(progressBar);
}
...
return view;
}
}
英文:
The reason why you get crash is because the progressBar
is not properly created. You are just adding null Object into your progressBarList
.
So to solve the Exception, you have to assign a ProgressBar
to it:
public class MiddleScreenfragment extends Fragment {
private FragmentMiddleScreenfragmentBinding binding;
List<ProgressBar> progressBarList = new ArrayList<ProgressBar>();
ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
// Assign progressBar so that it is not null
progressBar = new ProgressBar(getContext());
...
}
}
And to assign ID programmatically, you can make use of this function View.generateViewId()
:
> Generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.
And you can use it like this:
public class MiddleScreenfragment extends Fragment {
private FragmentMiddleScreenfragmentBinding binding;
List<ProgressBar> progressBarList = new ArrayList<ProgressBar>();
ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentMiddleScreenfragmentBinding.inflate(inflater,container,false);
View view = binding.getRoot();
for (int i = 0; i < 10; i++) {
progressBar = new ProgressBar(getContext());
// Set ID programmatically of the newly created Progress Bar
progressBar.setId(View.generateViewId());
progressBarList.add(progressBar);
}
...
return view;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论