Android Intent in AsyncTask class giving error

2019-08-06 03:32发布

I am new to Android programming and trying to learn the concepts, of course with the help from my fellow Stackoverflow user.

What i am trying to do: From the Login page, I am trying to fetch the login credentials from a json file located on a server. After parsing the json file and verifying the login credentials, i am starting a new intent to the next page of my app(Home).

Problem: I have successfully done all the networkings operations and parsing on a background thread using AsyncTask, as suggested by stackoverflow users. But, after verifying the login credentials, when i try to start a new intent inside the postExecute method of AsyncTask class, it gives me the following error. Please suggest what is wrong.

Error:

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

Error in line:

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

code:

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) {


        }
    }

Looking for a solution. Thanks in advance.

4条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-06 03:56

try this

  Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
  startActivity(intent);
查看更多
小情绪 Triste *
3楼-- · 2019-08-06 04:08
Intent intent = new Intent(this,HomeActivity.class);

In above line, this refers to AsyncTask's instance, and Intent use Application or Activity Context as a Constructor argument.

So it should be like,

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

in your case,

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

or

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

Here mContext is a Activity Context which is calling AsyncTask. You can pass this context to AsyncTask's Constructor.

查看更多
We Are One
4楼-- · 2019-08-06 04:14

The problem is here -

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

In the above code - this refers to the AsyncTask class .

Instead you can use LoginActivity.this or the context .

Intent intent = new Intent(LoginActivity.this,HomeActivity.class);
查看更多
等我变得足够好
5楼-- · 2019-08-06 04:15

Try this

startActivity(new Intent(LoginActivity.this, HomeActivity.class));
查看更多
登录 后发表回答