您好,我有动态表单在应用程序中显示。表单还包含我需要从Web服务获取每个页面的问题的部分(页面)。为此,我得到父活动上每个表单的节数。从活动我将一个表单的页面列表传递给viewPager适配器,然后我们通过重用相同的片段,根据来自Activity的列表动态创建页面数。我的应用程序在创建viewPager的最后一页时崩溃。
获取表单数据和每种形式的页面
private void getFormsList()
{
final ProgressDialog progressDialog = getProgressDialog(MainActivity.this);
progressDialog.show();
RestClient.getInstanceWithToken(MainActivity.this, "", "https://rj-corp-survey-app.herokuapp.com/").getForms().enqueue(new Callback<GetFormsListResponse>() {
@Override
public void onResponse(Call<GetFormsListResponse> call, Response<GetFormsListResponse> response) {
GetFormsListResponse getFormsListResponse = response.body();
if(getFormsListResponse != null && getFormsListResponse.equals("null") == false && getFormsListResponse.isSuccess() == true && getFormsListResponse.getCode() == 200)
{
formsArrayList = getFormsListResponse.getData().getForms();
System.out.print("forms list>>>"+ formsArrayList);
Toast.makeText(MainActivity.this, getFormsListResponse.getMessage(), Toast.LENGTH_SHORT).show();
if(formsArrayList != null && formsArrayList.size()>0)
{
for(int i=0; i<formsArrayList.size(); i++)
{
formCode = formsArrayList.get(i).getFormShortCode();
formCodeList.add(formCode);
sectionsArrayList = formsArrayList.get(i).getSections();
if(i==0)
{
distributorFormId = formsArrayList.get(i).get_id();
for(int j=0; j<formsArrayList.get(i).getSections().size(); j++)
{
String distributorForm = formsArrayList.get(i).getSections().get(j).getSectionShortCode();
DistributorSectionNames.add(distributorForm);
String sectionId = formsArrayList.get(i).getSections().get(j).get_id();
distributorSectionIdList.add(sectionId);
}
}
if(i==1)
{
outletFormId = formsArrayList.get(i).get_id();
for(int j=0; j<formsArrayList.get(i).getSections().size(); j++)
{
String outletForm = formsArrayList.get(i).getSections().get(j).getSectionShortCode();
OutletSectionNames.add(outletForm);
String sectionId = formsArrayList.get(i).getSections().get(j).get_id();
outletSectionIdList.add(sectionId);
}
}
if(i==2)
{
spokeFormId = formsArrayList.get(i).get_id();
for(int j=0; j<formsArrayList.get(i).getSections().size(); j++)
{
String spokeForm = formsArrayList.get(i).getSections().get(j).getSectionShortCode();
SpokeSectionNames.add(spokeForm);
String sectionId = formsArrayList.get(i).getSections().get(j).get_id();
spokeSectionIdList.add(sectionId);
}
}
if(sectionsArrayList != null && sectionsArrayList.size()>0)
{
for(int j=0; j<sectionsArrayList.size(); j++)
{
sectionCode = sectionsArrayList.get(j).getSectionShortCode();
sectionCodeList.add(sectionCode);
}
}
}
}
else
{
Toast.makeText(MainActivity.this, "No Forms Present", Toast.LENGTH_SHORT).show();
}
}
else if(getFormsListResponse != null && getFormsListResponse.equals("null") == false)
{
Toast.makeText(MainActivity.this, getFormsListResponse.getDescription(), Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Invalid request", Toast.LENGTH_SHORT).show();
}
populateData(formCodeList,distributorFormId, distributorSectionIdList);
progressDialog.dismiss();
}
@Override
public void onFailure(Call<GetFormsListResponse> call, Throwable t) {
Log.d("", "failure: " + t.getMessage());
progressDialog.dismiss();
Toast.makeText(MainActivity.this, t.getMessage() + "", Toast.LENGTH_SHORT).show();
}
});
}
将页面列表传递给适配器
private void populateData(ArrayList<String> formCodeList, String distributorFormId, ArrayList<String> distributorSectionIdList)
{
ViewPager pager = findViewById(R.id.viewPager);
pager.setAdapter(new FormAdapter(getSupportFragmentManager(), formCodeList, distributorFormId, distributorSectionIdList));
}
通过重用相同的片段
动态创建每个表单的页数
public Fragment getItem(int position) {
return FormFragment.newInstance(position + 1, getCount(), sections.get(position),distributorFormId, distributorSectionIdList.get(position) );
}
newInsnce()body
public static FormFragment newInstance(int index, int total, String name, String distributorFormId, String distributorSectionId ) {
FormFragment f = new FormFragment();
Bundle args = new Bundle();
args.putInt(KEY_INDEX, index);
args.putInt(KEY_TOTAL, total);
args.putString(KEY_NAME, name);
args.putString(KEY_FORM_ID, distributorFormId);
args.putString(KEY_SECTION_ID, distributorSectionId);
f.setArguments(args);
return f;
}
错误日志
FATAL EXCEPTION: main
Process: com.ampersand.testdynamicforms, PID: 27738
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6494)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3109)
at android.view.View.measure(View.java:16525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16525)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16525)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1630)
at android.view.View.measure(View.java:16525)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:16525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2358)
at android.view.View.measure(View.java:16525)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1962)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1159)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1341)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1046)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5660)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:9