0

ボタンがクリックされたときにサーバーへのhttpGetを実行するために単純なスレッドを使用していますが、実行後にこれを取得します。

Button b_back = (Button) findViewById(R.id.bback);
b_back.setOnClickListener(this);
Button b_sign_up = (Button) findViewById(R.id.signup_button);
b_sign_up.setOnClickListener(this);

@Override
public void onClick(View arg0) 
{
    // TODO Auto-generated method stub
    switch (arg0.getId()) 
    {
        case R.id.bback:
            Intent i = new Intent(this, MainSwitch.class);
            finish();
            startActivity(i);
            break;

            // More buttons go here (if any) ...

        case R.id.signup_button:
            if(username.getText().toString().equalsIgnoreCase("") ||
               password.getText().toString().equalsIgnoreCase("") ||
               email.getText().toString().equalsIgnoreCase(""))
            {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setMessage("Please fill in all the gaps!");
                dialog.show();
            }
            else
            {
                //****** Call method that sends the information to server.
                Thread background = new Thread (new Runnable() 
                {
                    public void run()
                    {
                        // Call the time consuming method
                        handler.sendEmptyMessage(0);
                    }
                });
                background.start();
            }
    }
}

private Handler handler = new Handler() 
{
    @Override
    public void handleMessage(Message msg) 
    {
        Toast.makeText(getApplicationContext(),
                       "Done thread",
           Toast.LENGTH_SHORT).show();

    }
};
4

1 に答える 1

7

あなたが得るエラーライン:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

通常、非 UI スレッドで UI 要素に何かをしようとする問題に関連しています。

コードの一部を省略したと述べ// Call the time consuming methodていると思います。この時間のかかるメソッドが定期的に実行されるという事実Threadは、UI 要素と対話できないことを意味します。

さらに多くのコード (およびエラーが発生した行を指定するコード) を投稿すると、解決方法に関する詳細情報を提供できる可能性があります。

于 2011-12-16T12:16:24.490 に答える