AsyncTask 类中的 Android Intent 给出错误

标签 android android-intent android-asynctask

我是 Android 编程的新手,正在尝试学习这些概念,当然是在我的 Stackoverflow 用户同伴的帮助下。

我正在尝试做什么:在登录页面,我试图从位于服务器上的 json 文件中获取登录凭据。解析 json 文件并验证登录凭据后,我开始对我的应用程序(主页)的下一页进行新的 Intent 。

问题:按照 stackoverflow 用户的建议,我已经使用 AsyncTask 在后台线程上成功完成了所有网络操作和解析。但是,在验证登录凭据后,当我尝试在 AsyncTask 类的 postExecute 方法中启动新 Intent 时,它会出现以下错误。请提出错误的建议。

错误:

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


        }
    }

寻找解决方案。提前致谢。

最佳答案

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

在上一行中,this 指的是 AsyncTask 的实例Intent 使用 Application 或 Activity Context 作为构造函数参数。

应该是这样的

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 的 Activity Context。您可以将此上下文传递给 AsyncTask 的构造函数。

关于AsyncTask 类中的 Android Intent 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14869181/

相关文章:

android - 如何将数据从一个应用程序发送到android中的另一个应用程序?

android - 使用安卓 :action to launch activity from settings

java - android Wolfram alpha 异常下载网址

java - Android Opengl es 1 - asynctask 在某些设备上导致奇怪的行为

android - 如何设置报警信息?

android - 推特喜欢 ListView

java - FLAG_ACTIVITY_NEW_TASK不打开以前的 Activity ,而仅在新安装apk时打开

android - 嵌套的 AsyncTask 和 onPostExecute

java - 使用 LibGDX 和 BaseGameUtils 创建 gameHelper 时如何修复空指针异常?

android - 用phoneGap播放声音时出错