2013-02-14 45 views
1

我是新來的Android編程和嘗試學習的概念,當然在我的同胞Stackoverflow用戶的幫助。Android Intent中AsyncTask類給出的錯誤

我在做什麼:從登錄頁面,我試圖從位於服務器上的json文件中獲取登錄憑證。解析json文件並驗證登錄憑據後,我開始了一個新的意圖到我的應用程序(Home)的下一頁。

問題:我已經成功做了所有networkings操作和分析使用的AsyncTask後臺線程,通過計算器用戶的建議。但是,驗證登錄憑證後,當我嘗試在AsyncTask類的postExecute方法內啓動新的意圖時,它會給我以下錯誤。請提出什麼是錯的。

錯誤

The constructor Intent(LoginActivity.RetreiveDataAsynctask, Class<HomeActivity>) is undefined 

錯誤行:

Intent intent = new Intent(this,HomeActivity.class); 

代碼

private class RetreiveDataAsynctask extends AsyncTask<String,Void,JSONObject> { 

     @Override 
     protected JSONObject doInBackground(String... loginURL) { 
      JSONObject obj = getJSONfromURL(loginURL[0]); 
      return obj; 
     } 

     @Override 
     protected void onPostExecute(JSONObject obj) { 
      //Do all ur UI related stuff as this will be executing in ui thread 

      super.onPostExecute(obj); 

      TextView loginStatus = (TextView) findViewById(R.id.login); 
      TextView userId = (TextView) findViewById(R.id.userId); 

      //if login successful, then go to the next page. 

      try { 
       loginStatus.setText(obj.getString("LOGIN")); 
       userId.setText(obj.getString("userId")); 

       AlertDialog.Builder adb = new AlertDialog.Builder(
         LoginActivity.this); 
         adb.setTitle("Login Service"); 
         adb.setPositiveButton("Ok", null); 

       if(obj.getString("LOGIN").equalsIgnoreCase("TRUE") && !obj.getString("userId").equalsIgnoreCase("")){ 
        //call next page 
        adb.setMessage("Login Success...!!"); 

        Intent intent = new Intent(this,HomeActivity.class); **//THIS LINE SHOWS ERROR** 
        startActivity(intent); 



       }else{//login unsuccessful 
        adb.setMessage("Login Failed...!!"); 

       } 

       adb.show(); 


      } catch (JSONException e) { 
       Log.e("log_tag", "JSONException "+e.toString()); 
      } 
     } 


     @Override 
     protected void onPreExecute() { 

      TextView loginStatus = (TextView) findViewById(R.id.login); 
      TextView userId = (TextView) findViewById(R.id.userId); 
      loginStatus.setText("Not Fetched"); 
      userId.setText("Not Fetched"); 

     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 


     } 
    } 

尋找一個SOLUT離子。提前致謝。

回答

2
Intent intent = new Intent(this,HomeActivity.class); 

在上述線,this的AsyncTask的實例,和Intent使用應用程序或活動上下文作爲構造器參數。

因此,它應該是這樣,

Intent intent = new Intent(<Your_Calling_Activity_Name.this>,HomeActivity.class); 
你的情況

Intent intent = new Intent(LoginActivity.this,HomeActivity.class); 

Intent intent = new Intent(mContext,HomeActivity.class); 

這裏mContext是呼籲AsyncTask一個活動上下文。您可以將此上下文傳遞給AsyncTask的構造函數。

+0

THX這麼快回復!完美工作!選擇這個作爲正確的答案。 Thx再次! – 2013-02-14 06:49:21

+0

樂意幫忙..!快樂編碼..! – user370305 2013-02-14 06:49:52

0

的問題是在這裏 -

Intent intent = new Intent(this,HomeActivity.class); 

在上面的代碼 - 這指的AsyncTask類。

相反,您可以使用LoginActivity.this或上下文。

Intent intent = new Intent(LoginActivity.this,HomeActivity.class); 
1

試試這個

Intent intent = new Intent(getApplicationContext(),HomeActivity.class); 
    startActivity(intent); 
+0

也試過這個。完美工作。 Thx快速回復。 – 2013-02-14 06:55:24

0

試試這個

startActivity(new Intent(LoginActivity.this, HomeActivity.class));