如何使用翻新API Quiz App在arrayList中显示第一个问题

时间:2018-10-29 03:32:59

标签: android android-studio android-fragments

我有一个Quiz项目,想显示多个选择问题,这里我使用两个活动,一个片段和改造API,但是当我要显示第一个问题时,它总是一个错误,而在下一个出现第二个问题时,

我在此活动中称为StartQuestion的第一个活动是我使用改造从API调用数据

in

在这里,我称为翻新API

O(N)

从StartActivity获得数据后,我进行了一个活动和一个片段来显示问题。我叫SelfLeraningQuizActivity和SelfLearningQuizFragment

SelfLearningQuizActivty代码:

public class StartQuestionActivity extends AppCompatActivity implements View.OnClickListener {

Button start;
ArrayList<AssessmentResponse.Soal> dataSoalList;
SendData mSend;
String listSoal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_question);


    fetchRemote();
    start = findViewById(R.id.btnStart);
    start.setOnClickListener(this);
}
 @Override
    public void onClick(View view) {
        Intent intent= new Intent(this,SelfLearningQuizActivity.class);
        intent.putExtra("listSoal",listSoal);
        startActivity(intent);
    }

如果我使用代码无法显示第一个问题和nullpointer

 public void fetchRemote(){

    ApiCaller apiCaller = new ApiCaller();

    apiCaller.withListener(new OnApiListener<AssessmentResponse>() {
        @Override
        public void onApiSuccess(AssessmentResponse assessmentResponse) {
            dataSoalList = assessmentResponse.getResponData().getSoal();
            Gson gson = new Gson();
            listSoal = gson.toJson(dataSoalList);

        }

        @Override
        public void OnApiFailur(@NotNull AppError error) {
            Toast.makeText(getApplicationContext(),"Gagal",Toast.LENGTH_SHORT).show();

        }
    }).getAssesment(requestBody.build(),getApplicationContext());
}

,并且第一个问题显示此错误代码:

public class SelfLearningQuizActivity extends AppCompatActivity implements View.OnClickListener, StartQuestionActivity.SendData/*, SelfLearningQuizFragment.OnRadioGroupSelectedListener*/ {

private int mfragmentIndex=0;
private ViewPager viewPager;
public static int obtainedScore=0;
private Question currentQuestion;
private List<Question> questionsList;
private HashMap<String,Object> hashMap;
private ArrayList<AssessmentResponse.Soal> dataSoalList = new ArrayList<>() ;
private Button btnBack,btnNext;

private QuestionCallbacks mQuestionCallbacks;
private List<QuestionCallbacks> questionCallbacks = new ArrayList<>();

@Override
public void onSend(ArrayList<AssessmentResponse.Soal> listSoal) {
    /*dataSoalList = listSoal;
    Toast.makeText(this,dataSoalList.size(),Toast.LENGTH_LONG).show();*/
}

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_self_learning_quiz);
    btnBack = findViewById(R.id.btnBack);
    btnNext = findViewById(R.id.btnNext);

    viewPager = findViewById(R.id.view_pager);

    btnBack.setOnClickListener(this);
    btnNext.setOnClickListener(this);

    dataSoalList = new ArrayList<>();
    String listSoalString = getIntent().getStringExtra("listSoal");
    Gson gson = new Gson();
    Type type = new TypeToken<ArrayList<AssessmentResponse.Soal>>(){}.getType();
    ArrayList<AssessmentResponse.Soal> soalList = gson.fromJson(listSoalString, type);
    dataSoalList.addAll(soalList);
    setupViewPager(viewPager);

如果我不使用代码,则显示第一个问题 结果:enter image description here

第一个问题无法显示,但第二个问题显示: enter image description here

设置ViewPager和Onclick:

  if(mfragmentIndex == 0){
            questionCallbacks.get(mfragmentIndex).onSetQuestionData(dataSoalList.get(mfragmentIndex));

 }
    }

我使用界面将数据问题从“活动”发送到片段

 java.lang.RuntimeException: Unable to start activity ComponentInfo{brismart.bri.co.id/brismart.bri.co.id.SelfLearningQuizActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:160)
                  at android.app.ActivityThread.main(ActivityThread.java:6248)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:788)

这是我在SelfLearningQuizFragment中显示的问题

private void setupViewPager(ViewPager viewPager) {

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    int index = 0;
    for (AssessmentResponse.Soal soal:dataSoalList) {
        SelfLearningQuizFragment selfLearningQuizFragment = SelfLearningQuizFragment.newInstance(String.valueOf(index++));
        mQuestionCallbacks = selfLearningQuizFragment;
        adapter.addFrag(selfLearningQuizFragment,"Question"+index++);
        questionCallbacks.add(mQuestionCallbacks);
    }

    viewPager.setOffscreenPageLimit(adapter.getCount());
    viewPager.setAdapter(adapter);
}

@Override
public void onClick(View view) {
    if(view == btnBack){
        mfragmentIndex--;
        viewPager.setCurrentItem(mfragmentIndex);
    }else if(view == btnNext){
        mfragmentIndex++;
        viewPager.setCurrentItem(mfragmentIndex);
        questionCallbacks.get(mfragmentIndex).onSetQuestionData(dataSoalList.get(mfragmentIndex));
    }
}

对此有什么想法吗? 谢谢你的帮助

0 个答案:

没有答案