当前位置: 首页 > 知识库问答 >
问题:

只有创建视图层次结构的原始线程才能接触其视图。Android

鲁霄
2023-03-14
public class master extends Activity {
    ProgressDialog    progressDialog;
    EditText tahmini_kelime;
    EditText girilen_sayi ;
    EditText toplam_harf_sayisi ;
    Button tamamdir; 
    TextView jTextArea1;
    Vector vector_all,vect_end,vect,recent_search;
    BufferedReader read;
    String recent_tahmin_kelime; 
    boolean bayrak,bayrak2 ;
    int column_number  ;
    InputStreamReader inputreader ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.master);
        column_number=0;
        bayrak=true;
          toplam_harf_sayisi=(EditText)findViewById(R.id.toplam_harf);
          tahmini_kelime=(EditText)findViewById(R.id.tahmini_kelime);
          girilen_sayi=(EditText)findViewById(R.id.sayi_gir);
          tamamdir=(Button)findViewById(R.id.tamamdirrrr);
            jTextArea1=(TextView)findViewById(R.id.jte);
            bayrak2=true;




            recent_search = new Vector();
            InputStream  inputStream =  getResources().openRawResource(R.raw.sozluk);
            try {
                  inputreader     = new InputStreamReader(inputStream,"utf-8");
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            };
            read = new BufferedReader(inputreader);


            int k = 0;
            String result = "";
            try {

                vector_all = new Vector();
                while (read.ready()) {

                    result = read.readLine();
                    vector_all.add(result);
                    jTextArea1.append(result + "\n");
                    k = k + 1;
                }


                String size = "" + k;


            } catch (IOException ex) {
            }




          tamamdir.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {




                if( bayrak2 )
                {  
                    if(Integer.parseInt(toplam_harf_sayisi.getText().toString())>8 || Integer.parseInt(toplam_harf_sayisi.getText().toString())<=1)
                    { 
                            toplam_harf_sayisi.setText("");
                            Dialog  dl=new Dialog(master.this);
                            dl.setTitle("hatalı giriş");
                            dl.setCanceledOnTouchOutside(true);
                            dl.show(); 
                            return;
                    }


                     int findwordlength = Integer.parseInt(toplam_harf_sayisi.getText().toString());

                        int k = 0;
                        String result = "";
                        jTextArea1.setText("");
                        InputStream  inputStream =  getResources().openRawResource(R.raw.sozluk);
                        try {
                              inputreader     = new InputStreamReader(inputStream,"utf-8");
                        } catch (UnsupportedEncodingException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        };
                        read = new BufferedReader(inputreader);

                        String resultword = "";
                        try {

                            vect = new Vector();



                             while (read.ready()) {

                                result = read.readLine();

                                if (result.length() == findwordlength) {

                                    vect.addElement(result);
                                    resultword = resultword + result + "\n";
                                    k = k + 1;
                                }
                                jTextArea1.setText("");
                            }



                            jTextArea1.append(resultword + "\n");

                            RandomKelime(vector_all,0 );



                        } catch (IOException ex) {
                        }

                        toplam_harf_sayisi.setEnabled(false);
                        girilen_sayi.setEnabled(true);
                        bayrak2=false;
                }
                else
                { 

                    progressDialog = ProgressDialog.show(master.this, "Bir Düşüneyim :D", "lütfen bekleyiniz...");
                    Thread thread = new Thread(new Runnable() {
                        public void run() {


                     mainGuessWord(column_number);


                     handler.sendEmptyMessage(0);
                        }
                    });
                    thread.start();  

                     girilen_sayi.setText("");

                         }



                        }




        });





    }

    private void mainGuessWord(int look) {
        int result_int = 0;
        String randomword = "";
        int randomword2 = 0;


        randomword = tahmini_kelime.getText().toString();


            result_int = Integer.parseInt(girilen_sayi.getText().toString());


        if (result_int == 0) {
            mevcut_degil(vect, randomword);
        } else {
            elemeAgaci(vect, randomword, result_int);
        }

    }


    public void elemeAgaci(Vector vect, String elem, int length) {
        String word = elem.toString();
        Vector cmp_vect;
        cmp_vect = new Vector();
        vect_end = new Vector();
        int count = 0;
        int countword = 0;  // toplam word sayısı
        int each_word_total = 0; // her kelimede bulunan harf sayısı
        jTextArea1.setText("");

            String compare = "";
            for (int i = 0; i < vect.size(); i++) {
                each_word_total = 0;
                compare = "";
                for (int j = 0; j < word.length(); j++) {

                    if(!compare.contains(""+word.charAt(j)))
                    {
                   for (int k = 0; k < vect.get(i).toString().length(); k++) {
                        if (vect.get(i).toString().charAt(k) == word.charAt(j)) {
                            each_word_total++;

                        }

                    }


                   compare=""+compare+word.charAt(j);
                    }

                }
                System.out.println("" + vect.get(i) + "  =>  " + each_word_total);

                if (length == each_word_total) {
                    cmp_vect.add(vect.get(i));
                    jTextArea1.append(vect.get(i) + "\n");
                    countword++;
                }

            }


            vect.clear();
            for (int l = 0; l < cmp_vect.size(); l++) {
                vect.add(cmp_vect.get(l));
            }




        if (countword == 1) {
                Dialog  dl=new Dialog(master.this);
                dl.setTitle("The Word id : "+jTextArea1.getText().toString());
                dl.setCanceledOnTouchOutside(true);
                dl.show(); 

        } else {
            column_number = column_number + 1;
            if(vect.size()<10){
            RandomKelime_Table(vect);
            }else{
            RandomKelime(vector_all, column_number);
            }
        }
    }

    public void mevcut_degil(Vector vect, String m) {
        char control[];
        control = m.toCharArray();
        boolean flag = false;
        int countword = 0;
        Vector detect;
        detect = new Vector();

        jTextArea1.setText("");
        for (int k = 0; k < vect.size(); k++) {
            flag = false;
            for (int s = 0; s < control.length; s++) {
                if (vect.get(k).toString().contains("" + control[s])) {
                    flag = true;
                }

            }
            if (!flag) {
                detect.addElement(vect.get(k));
                countword = countword + 1;
            }

        }
        vect.clear();
        for (int s = 0; s < detect.size(); s++) {
            vect.addElement(detect.get(s));
        }
        for (int a = 0; a < countword; a++) {
            jTextArea1.append(vect.get(a).toString() + "\n");
        }

        if (countword == 1) {
                Dialog  dl=new Dialog(master.this);
                dl.setTitle("The Word id : "+jTextArea1.getText().toString());
                dl.setCanceledOnTouchOutside(true);
                dl.show();
        } 

        else {
            column_number = column_number + 1;
            RandomKelime(vect, column_number);
        }
    }





    public void RandomKelime(Vector vector, int k)
    {
        String sesli[]={"a","e","ı","i","o","ö","u","ü"};

        Random a = new Random();

        if (k == 0) { 
            String passedword = "";
            passedword = vector_all.get((int) (Math.random() * vector_all.size())).toString();

            while (passedword.length() < 8) {
                passedword = vector_all.get((int) (Math.random() * vector_all.size())).toString();

            }

            tahmini_kelime.setText(passedword);
            recent_tahmin_kelime=passedword;

       // jTable1.setValueAt(vector_all.get((int) (Math.random() * vector_all.size())), k, 0);
        } else {
        recent_search.addElement(recent_tahmin_kelime );
        int say = 0;
        String design = "";
        String guess_words = "";
        String as="";
        int f=0;
        int count=0;
        int calculate_all=0;

        for (int u = 0; u < recent_search.size(); u++) {
        design = recent_search.get(u).toString();
        bayrak = false;
        as="";
        count=0;
        for(int  s=0;s<sesli.length;s++)
        {
        if(design.contains(""+sesli[s]) && count==0){
        as+=""+sesli[s];
        count++;
        }

        }

        guess_words = vector_all.get((int) a.nextInt(vector_all.size())).toString();

           while (guess_words.length() < 8) {
                guess_words = vector_all.get((int) (Math.random() * vector_all.size())).toString();

            }




        while (say < design.length()) {
          calculate_all=0;
        while (guess_words.contains("" + as) && !design.equals(guess_words)) {
        say = 0;
        calculate_all++;
        guess_words = vector_all.get( a.nextInt(vector_all.size())).toString();

          while (guess_words.length() < 8) {
                guess_words = vector_all.get((int) (Math.random() * vector_all.size())).toString();

            }



        f=f+1;
        System.out.println("Tahmın: " + guess_words + " => " + design);
        if(calculate_all>vect.size())
        {
        break;
        }
        }
        say++;
        System.out.println("coutn: " + say);
        }
        }


        if (true) {
        tahmini_kelime.setText(guess_words);
        }


        } 
    } 

     public void RandomKelime_Table(Vector vector ) {
            String passedword = "";
            Random a = new Random();

            try {


            passedword = vect.get(a.nextInt(vect.size())).toString();
            } catch (Exception e) {
                    Dialog  dl=new Dialog(master.this);
                    dl.setTitle("Hatalı Giriş.Yeniden Başlayın.");
                    dl.setCanceledOnTouchOutside(true);
                    dl.show(); 
                yeniden_basla();
            }
            tahmini_kelime.setText(passedword );

     }

     public void yeniden_basla()
     {  
         bayrak2=true;
         girilen_sayi.setEnabled(false);
         toplam_harf_sayisi.setEnabled(true);
         toplam_harf_sayisi.setText("");
         vect.clear();
         vector_all.clear();
         vect_end.clear();
         recent_search.clear();
         jTextArea1.setText("");
         recent_tahmin_kelime="";
         column_number=0;
         bayrak=true;
         InputStream  inputStream =  getResources().openRawResource(R.raw.sozluk);
            try {
                  inputreader     = new InputStreamReader(inputStream,"utf-8");
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            };
            read = new BufferedReader(inputreader);


            int k = 0;
            String result = "";
            try {

                vector_all = new Vector();
                while (read.ready()) {

                    result = read.readLine();
                    vector_all.add(result);
                    jTextArea1.append(result + "\n");
                    k = k + 1;
                }


                String size = "" + k;


            } catch (IOException ex) {
            }
     }



     private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                progressDialog.dismiss();


            }
        };  



}

这是我所有的代码。

共有1个答案

祝高超
2023-03-14

您不会显示在哪里创建处理程序(onCreate?onStart?其他地方?)。是从主线开始的吗?如果是这样,您需要提供更完整的堆栈跟踪,以便我们能够理解。

如果您是从另一个线程启动它,那么这就是问题所在,因为它试图更改progressDialog,而这必须从主线程完成。

PS:如果你使用了AsyncTask,你就不必为它费心了,因为它的设计目的就是要做你想要的事情,并且是线程安全的。

Post comment:使用异步线程:在onPreExecute()中添加进度条,将run()更改为doInBackground(),并将dismise()移动到onPostExecute(

 类似资料: