2014-03-02 119 views
0

我想將輸入的密碼值傳遞給一個對話框,並將它傳遞給asyntask,以便它可以發送到數據庫進行比較。我在密碼字段處得到nullpointerexception。看起來價值沒有通過。我該如何解決這個問題?將字符串傳遞給Asyntask

對話框,需要密碼才能繼續:

public void onClick(View view) { 
      LayoutInflater layoutInflater = LayoutInflater.from(context); 
      View promptView = layoutInflater.inflate(
        R.layout.prompt_password, null); 
      AlertDialog.Builder alert = new AlertDialog.Builder(context); 

      alert.setView(promptView); 

      // Set an EditText view to get user input 
      final EditText input = (EditText) promptView 
        .findViewById(R.id.passwordInput); 

      alert.setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          String pass = (input.getText()).toString(); 
          // Do something with value! 
          Log.d("Value", pass); 
          ProgressDialog progressDialog = new ProgressDialog(DisplayReqItemInfo.this); 
          progressDialog.setMessage("Cheking password..."); 

          ItemEdit itemEdit = new ItemEdit(DisplayReqItemInfo.this, progressDialog); 
          itemEdit.execute(); 
         } 
        }); 

      alert.setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          // Canceled. 
          dialog.cancel(); 
         } 
        }); 

      AlertDialog dialog = alert.create(); 
      dialog.show(); 

     } 
    }); 

} 

這是假設檢索值類:

public class ItemEdit extends AsyncTask<String, Void, Integer> { 

private ProgressDialog progressDialog; 
private DisplayReqItemInfo activity; 

private int responseCode = 0; 

public ItemEdit(DisplayReqItemInfo activity, ProgressDialog progressDialog) 
{ 
    this.activity = activity; 
    this.progressDialog = progressDialog; 
} 

@Override 
protected void onPreExecute() 
{ 
    progressDialog.show(); 
} 

protected Integer doInBackground(String... arg0) { 
    TextView PID = (TextView)activity.findViewById(R.id.req_pid); 
    EditText passwordEdit = (EditText)activity.findViewById(R.id.passwordInput); 
    String pid = PID.getText().toString(); 
    String password = passwordEdit.getText().toString(); 
    ItemFunction itemFunction = new ItemFunction(); 
    JSONObject json = itemFunction.requestItem(pid, password); 

    // check for response 
    try { 
     if (json.getString(KEY_SUCCESS) != null) { 
      String res = json.getString(KEY_SUCCESS); 

      if(Integer.parseInt(res) == 1){ 

       responseCode = 1; 

      }else{ 
       responseCode = 0; 
      } 
     } 

    } catch (NullPointerException e) { 
     e.printStackTrace(); 

    } 
    catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return responseCode; 
} 

@Override 
protected void onPostExecute(Integer responseCode) 
{ 
    if (responseCode == 1) { 
     progressDialog.dismiss(); 
     Intent i = new Intent(); 
     i.setClass(activity.getApplicationContext(), MainMenu.class); 
     activity.startActivity(i); 
    } 
    else { 
     progressDialog.dismiss(); 

    } 

} 
} 
+1

'itemEdit.execute( 「yourString」);'和在'doInBackground'上使用'arg0 [0]' –

回答

1

您可以在execute

ItemEdit itemEdit = new ItemEdit(DisplayReqItemInfo.this, progressDialog); 
    itemEdit.execute(new String[] { "yourstring" }); 

傳遞參數在你的AsyncdoInBackground

protected Integer doInBackground(String... arg0) { 
String response = args0[0]; 
TextView PID = (TextView)activity.findViewById(R.id.req_pid); 
+0

因爲'doInBackground'有一個varargs參數,所以你可以只傳遞''yourString''作爲參數而不是'new String [] {「yourstring」}'。 –

0

從AsyncTask.doInBackground函數內部不能有屏幕I/O。您可以在可以訪問UI線程的onPreExecute或onPostExecute函數中執行屏幕I/O。因此,您可以從onPreExecute中的EditText字段獲取值,並將其放入一個全局爲AsyncTask方法並由doInBackground讀取的變量。

(使用你的例子)

public class ItemEdit extends AsyncTask<String, Void, Integer> { 
String password; 
String pid; 

    @Override 
    protected void onPreExecute() 
    { 
    TextView PID = (TextView)activity.findViewById(R.id.req_pid); 
    EditText passwordEdit = (EditText)activity.findViewById(R.id.passwordInput); 

    pid = PID.getText().toString(); 
    password = passwordEdit.getText().toString(); 
    } 

    protected Integer doInBackground(String... arg0) { 
    ItemFunction itemFunction = new ItemFunction(); 
    JSONObject json = itemFunction.requestItem(pid, password); 

    . . . 
    } 
} 

您也可以傳遞參數到的AsyncTask初始化,並通過一流的全球通:

TextView PID = (TextView)activity.findViewById(R.id.req_pid); 
EditText passwordEdit = (EditText)activity.findViewById(R.id.passwordInput); 

String pid = PID.getText().toString(); 
String password = passwordEdit.getText().toString(); 

// start background task 
mAuthTask = new UserLoginTask(pid, password); 
mAuthTask.execute(); 


public class UserLoginTask extends AsyncTask<Void, Void, Boolean> { 
    private String userLoginId; 
    private String userPassword; 

    private UserLoginTask(String loginId, String password) { 
    userLoginId = loginId; 
    userPassword = password; 
    } 

    @Override 
    protected Boolean doInBackground(Void... params) { 

     . . . 

     // Do something with userLoginId, userPassword 

     . . . 
    } 
}