我制作了一個應用程序,它從兩個文本文件(一個帶有單詞,另一個帶有定義)中獲取單詞和定義。使用隨機數生成要顯示的單詞,然后將 5 個定義與正確的定義一起隨機排列以顯示選項。該應用程序運行完美,但當使用列表視圖單擊任何選項時,該應用程序將停止工作。ArrayList<String> word=new ArrayList<>();List<String> dfn=new ArrayList<>();String que="",ans="";int counter=0;private void random(){ Random num = new Random(); int nw = num.nextInt(word.size()); que = word.get(nw); ans = dfn.get(nw); dfn.remove(ans); Collections.shuffle(dfn); dfn = dfn.subList(0,4); dfn.add(ans); Collections.shuffle(dfn);}TextView t;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); Scanner sc = new Scanner(getResources().openRawResource(R.raw.word2)); Scanner sc2 = new Scanner(getResources().openRawResource(R.raw.def2)); while(sc.hasNextLine()&&sc2.hasNextLine()){ String a = sc.nextLine(); String b = sc2.nextLine(); word.add(a); dfn.add(b);我想上面的部分很好。 } sc.close(); sc2.close(); random(); t = (TextView) findViewById(R.id.t); t.setText(que); run();}ArrayAdapter<String> adap;public void run(){ ListView list = (ListView) findViewById(R.id.li); adap = new ArrayAdapter<>( this, android.R.layout.simple_list_item_1, dfn ); list.setAdapter(adap); list.setOnItemClickListener((adapterView, view,i,l)->{//lambda expression if(dfn.get(i).equals(ans)){ Toast.makeText(getApplicationContext(),"Correct!",Toast.LENGTH_SHORT).show(); counter++; } else{ Toast.makeText(getApplicationContext(),"Wrong!",Toast.LENGTH_SHORT).show(); } TextView t2 = (TextView) findViewById(R.id.t2); t2.setText("Score : "+counter); random(); t.setText(que); run(); } );}}
應用程序停止工作:單擊列表選項時,猜字游戲停止工作
幕布斯6054654
2021-10-27 16:55:50