Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法更改我的测验应用程序的问题_Java_Android - Fatal编程技术网

Java 无法更改我的测验应用程序的问题

Java 无法更改我的测验应用程序的问题,java,android,Java,Android,我目前正在用android制作一个测验应用程序,但我的代码中有一个大问题,那就是它无法根据ArrayList更改问题 当我单击ImageButton检查文本视图是否等于给定单词时,我的代码应该会更改问题。但是当我单击ImageButton时,showMeNextQuestion()不起作用 这是我的测验。java: package com.example.activity.Main; import android.app.AlertDialog; import android.content.

我目前正在用android制作一个测验应用程序,但我的代码中有一个大问题,那就是它无法根据
ArrayList
更改问题

当我单击
ImageButton
检查文本视图是否等于给定单词时,我的代码应该会更改问题。但是当我单击
ImageButton
时,
showMeNextQuestion()
不起作用

这是我的
测验。java

package com.example.activity.Main;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.activity.R;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class quiz extends AppCompatActivity{

    private ImageButton imageButton;
    private File soundfile = null;
    private GridView ll;
    private ImageButton imageButton1;
    private final ArrayList<String> buttonList = new ArrayList<>();
    ArrayAdapter<String> adapter;
    private TextView result , text_quiz;
    String lessons,TextCompare,speakTextTxt,tempDestFile ;
    HashMap<String, String> myHashRender = new HashMap<String, String>();
    int qtotal;
    private int counter=0;
    private int cont = 1;
    private int nextStartIndex=1;
    TextView number;
    List<String> word;
    Boolean answered;
    MediaPlayer mMediaPlayer = new MediaPlayer();
    TextToSpeech mTts;
    String word_to_use;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz);
        number = findViewById(R.id.numberss);
        imageButton = findViewById(R.id.icon1);
        result = findViewById(R.id.result);
        imageButton1 = findViewById(R.id.validate);
        text_quiz = findViewById(R.id.text_quiz);
        ll = findViewById(R.id.btnlay);
// ----------------------------------------------------------------------------------------------
        Intent intent1 = getIntent();
        final String ass = intent1.getStringExtra("lesson");
        word = new LinkedList<String>(Arrays.asList(ass.split("_")));
        Collections.shuffle(word);
        qtotal= word.size();

// ----------------------------------------------------------------------------------------------

        word_to_use = showmenextquestion();
        start(word_to_use);
        middle();
        audio();
    }
// end
// ----------------------------------------------------------------------------------------------




    private void start(String word_to_use) {




                    String arr[] = word_to_use.split(" ",2);
                    String fword = arr[0];
                    String rword = arr[1];
                    String reprword = rword.replaceAll("[a-z]", "*");
                    final String text = fword + " " + reprword;
                    SpannableString firstword = new SpannableString(text);
                    firstword.setSpan(new UnderlineSpan(), 0, fword.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    result.setText(firstword);

                    final String [] display = result.getText().toString().split("\\s");

                    final String[] words = word_to_use.split("\\s");
                    final int noOfBtns = words.length;
                    final Button[] btns = new Button[noOfBtns];
                    for(int i=0;i<noOfBtns;i++)
                    {
                        buttonList.add(words[i]);
                        Collections.shuffle(buttonList);
                        ll.removeAllViewsInLayout();
                        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, buttonList)
                        {
                            @Override
                            public View getView(int position, View convertView, ViewGroup parent) {
                                View row = null;
                                if(convertView == null)
                                {
                                    row = super.getView(position,convertView,parent);
                                    row.setBackgroundResource(R.drawable.btn_background);
                                }

                                return row;
                            }
                        };
                        ll.setAdapter(adapter);

                        ll.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            public void onItemClick(AdapterView parent, View v, int position, long id) {


                                if(position < noOfBtns) {

                                    String selectedItem = parent.getItemAtPosition(position).toString();
                                    String s2 = text_quiz.getText().toString();
                                    String [] s2_word = s2.split("\\s+");
                                    List<String> str = new LinkedList<String>(Arrays.asList(s2_word));
                                    if(str.remove(selectedItem))
                                    {
                                        StringBuilder builder = new StringBuilder();
                                        for(String details: str)
                                        {
                                            builder.append(details + " ");
                                        }
                                        text_quiz.setText(builder.toString());
                                            v.setBackgroundResource(R.drawable.btn_background);
                                    }else {

                                                v.setBackgroundResource(R.drawable.buttons4);
                                                text_quiz.append(selectedItem + " ");

                                    }
                                }

                                if (cont >= display.length) {
                                    cont = 0;
                                    nextStartIndex = 0;
                                }

                                result.setText(formatText(text, display[cont]));
                                cont++;

                            }
                        });

                }

            }
    private void middle() {
        imageButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String answer = showmenextquestion();


                    if(text_quiz.getText().toString().trim().equals(answer.trim()))
                    {

                        final AlertDialog.Builder builder = new AlertDialog.Builder(quiz.this);
                        builder.setTitle("Correct")
                                .setMessage("please click ok to continue until the end")
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        if(counter < qtotal)
                                        {
                                            showmenextquestion();

                                        }
                                        else
                                        {
                                            finish();
                                        }

                                    }
                                }).show();
                    }else
                    {
                        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                        builder.setTitle("Incorrect");
                        builder.setMessage("There is no Words.");
                        builder.setCancelable(true);

                        final AlertDialog dlg = builder.create();

                        dlg.show();

                        Handler mHandler = new Handler();
                        Runnable mRunnable = new Runnable () {

                            public void run() {
                                if(dlg != null && dlg.isShowing()) dlg.dismiss();
                            }
                        };
                        mHandler.postDelayed(mRunnable,1500);
                    }
                }

                /*if(TextCompare.compareTo(answer) != 0)
                {
                    new AlertDialog.Builder(quiz.this)
                            .setTitle("Correct")
                            .setMessage("please click ok to continue until the end")
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    text_quiz.setText("");
                                    ll.removeAllViewsInLayout();
                                    start();
                                    dialog.dismiss();
                                }
                            }).show();

                }else
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                    builder.setTitle("Incorrect");
                    builder.setMessage("Your Answer is Incorrect.");
                    builder.setCancelable(true);

                    final AlertDialog dlg = builder.create();

                    dlg.show();

                    Handler mHandler = new Handler();
                    Runnable mRunnable = new Runnable () {

                        public void run() {
                            if(dlg != null && dlg.isShowing()) dlg.dismiss();
                        }
                    };
                    mHandler.postDelayed(mRunnable,2000);
                    text_quiz.setText("");
                }*/

        });
    }
    private void audio() {

        speakTextTxt = showmenextquestion();
        myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, speakTextTxt);

        String exStoragePath = "";
        File appTmpPath = new File("/storage/emulated/0/Android/data/");
        boolean isDirectoryCreated = appTmpPath.mkdir();
        Log.d("MainActivity", "directory " + appTmpPath + " is created : " + isDirectoryCreated);
        String tempFilename = "tts.wav";
        tempDestFile = appTmpPath.getAbsolutePath() + File.separator + tempFilename;
        Log.d("MainActivity", "tempDestFile : " + tempDestFile);
        new MySpeech(speakTextTxt);

        imageButton.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                final String path = "/storage/emulated/0/Android/data/tts.wav";
                try {
                    mMediaPlayer.setDataSource(path);
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                } catch (IllegalStateException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    mMediaPlayer.prepare();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mMediaPlayer.setLooping(true);
                if(!mMediaPlayer.isPlaying())
                {
                    v.setBackgroundResource(R.drawable.ic_image_play);
                    mMediaPlayer.start();
                }
                else
                {
                    v.setBackgroundResource(R.drawable.ic_stop_black);
                    mMediaPlayer.pause();
                }
            }
        });

    }
    private String showmenextquestion() {
        counter = 1;
        if(counter < qtotal)
        {

                lessons = word.get(counter);
                TextCompare = word.get(counter);
                number.setText(counter + "/" + qtotal);

                counter++;

        }
        return lessons;
    }

    class MySpeech implements TextToSpeech.OnInitListener {

        String tts;

        public MySpeech(String tts)
        {
            this.tts = tts;
            mTts = new TextToSpeech(quiz.this, this);
        }

        @Override
        public void onInit(int status)
        {
            Log.v("log", "initi");
            int i = mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);
        }
    }
    public CharSequence formatText(String base, String highlight) {
        int start = base.indexOf(highlight,nextStartIndex);
        nextStartIndex = start+highlight.length();
        SpannableString span = new SpannableString(base);
        if (start >= 0) {

            span.setSpan(new UnderlineSpan(), start, nextStartIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return span;

        }

        return base;
    }

    @Override
    public void onBackPressed() {
        mMediaPlayer.stop();
       showmenextquestion();
    }
}

我的xml布局是横向的。请帮忙。谢谢。

变量计数器的值在if循环中递增,但当调用函数
showmenextquestion()
时,计数器值再次指定为1。因此它将始终仅显示第一个问题。 因此,使用
计数器=1在该功能之外,即放置
计数器=1
start()
audio()
函数中。因此,每当应用程序启动时,计数器值将为1,并在调用
showmenextquestion()
函数时递增

private String showmenextquestion() {
    if(counter < qtotal)
    {

            lessons = word.get(counter);
            TextCompare = word.get(counter);
            number.setText(counter + "/" + qtotal);

            counter++;

    }
    return lessons;
}
私有字符串showmenextquestion(){
如果(计数器<数量总和)
{
课程=单词.get(计数器);
text比较=word.get(计数器);
number.setText(计数器+“/”+数量总和);
计数器++;
}
回馈教训;
}

您不是在imageButton中调用showmenextquestion(),而是在imageButton1中调用它,单击列表是的,我试图在imageButton1中调用它,这是检查两个文本视图是否相等。因此计数器=1是导致此问题的原因。非常感谢,但是我在替换应用程序中的问题时仍然遇到了一些问题。代码实际上给出了下一个问题(即“number”textview正在移动),但仍然没有更改
private String showmenextquestion() {
    if(counter < qtotal)
    {

            lessons = word.get(counter);
            TextCompare = word.get(counter);
            number.setText(counter + "/" + qtotal);

            counter++;

    }
    return lessons;
}