0

「ウィンドウを追加できません — トークン null はアプリケーション用ではありません」というアラート ダイアログの作成に問題があります。

public class Authenticator {

public static final String TAG = "Authenticator";

public static int getUserId(final String username, final String password) { 

    int retVal = 0;

    final QuickTexterApplication qta = QuickTexterApplication.getQuickTexterApplication();
    final Handler handler =  new Handler();

    Thread thread = new Thread(new Runnable() {
        public void run() {                
            Runnable displayGUIRun = new Runnable() {
                public void run() {
                    int userId = 0;
                    HttpURLConnection urlConnection = null;
                    String urlAuthenticator = qta.getResources().getString(R.string.urlAuthenticator);                      
                    try{
                        URL url = new URL(urlAuthenticator);
                        urlConnection = (HttpURLConnection) url.openConnection();

                        urlConnection.setRequestMethod("POST");         
                        urlConnection.setDoInput(true);
                        urlConnection.setDoOutput(true);

                        DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());

                        wr.writeBytes("username=" + username + "&");
                        wr.writeBytes("password=" + password);
                        wr.flush();
                        wr.close();

                        urlConnection.connect();

                        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
                            Log.d(TAG,"HTTP OK");
                            InputStream inStream = urlConnection.getInputStream();
                            BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

                            String inLine = in.readLine();                  
                            in.close();

                            Log.d(TAG,"inLine: " + inLine);
                            userId = Integer.parseInt(inLine);
                        }
                        else {
                            Log.d(TAG,"HTTP NOT OK");
                        }

                        String alertMsg = "Unable to establish connection to server";
                        switch(userId){
                            case -1 :
                                alertMsg = "You entered an invalid username or password";
                            case 0  : // This is where the exception occurs
                                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(qta.getApplicationContext());
                                Log.d(TAG, alertMsg);
                                alertBuilder.setMessage(alertMsg)
                                            .setNeutralButton("Ok", new DialogInterface.OnClickListener(){
                                                @Override
                                                public void onClick(DialogInterface dialog, int id) {
                                                    dialog.cancel();
                                                }

                                            });
                                AlertDialog alert = alertBuilder.create();
                                alert.show();
                                break;

Authenticator は Avitvity ではないため、「this」をコンテキストとして使用することはできません。
しかし、その後、getApplicationContext() も機能していません...
私がやろうとしているのは、Activity クラスからメソッド getUserId() を呼び出すことです。

4

1 に答える 1

0

クラスにコンテキストを提供する Authenticator クラスにコンストラクターを作成する必要があります。このコンストラクターを呼び出して、このクラスにコンテキストを渡します。

  public class Authenticator {
       Context myContext;

   public Authenticator(YourActivity activity)
   {
    // TODO Auto-generated constructor stub
    this.myContext = activity;          
    }
  }

このようにして、このクラスのコンテキストを取得できます。うまくいくかどうか教えてください。:)

于 2012-07-27T13:09:17.887 に答える