Android的意图中的AsyncTask类给错误 [英] Android Intent in AsyncTask class giving error

查看:244
本文介绍了Android的意图中的AsyncTask类给错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid程序,并试图与我的同胞用户#1的帮助下学习的概念,当然。

什么,我试图做:从登录页面,我试图从位于服务器上的JSON文件获取登录凭据。解析JSON文件,并验证登录凭据后,我开始一个新的意图我的应用程序(主页)的下一个页面。

问题:我已经成功做了所有networkings操作和分析使用的AsyncTask后台线程,通过计算器用户的建议。但是,验证登录凭据后,当我尝试启动的AsyncTask类的postExecute方法中一个新的意图,它给了我下面的错误。请建议什么是错的。

错误

 构造意图(LoginActivity.RetreiveDataAsynctask,类< HomeActivity>)的不确定

错误行:

 意向意图=新意图(这一点,HomeActivity.class);

code

 私有类RetreiveDataAsynctask扩展的AsyncTask<弦乐,无效的JSONObject> {        @覆盖
        受保护的JSONObject doInBackground(字符串... loginURL){
            的JSONObject物镜= getJSONfromURL(loginURL [0]);
            返回OBJ;
        }        @覆盖
        保护无效onPostExecute(OBJ的JSONObject){
             //是否所有乌尔UI相关的东西,因为这将在UI线程执行            super.onPostExecute(OBJ);            TextView的loginStatus =(的TextView)findViewById(R.id.login);
            TextView的用户id =(的TextView)findViewById(R.id.userId);            //如果登录成功,然后进入下一个页面。            尝试{
                loginStatus.setText(obj.getString(登录));
                userId.setText(obj.getString(用户id));                AlertDialog.Builder ADB =新AlertDialog.Builder(
                        LoginActivity.this);
                        adb.setTitle(登录服务);
                        adb.setPositiveButton(OK,NULL);                如果(obj.getString(登录)equalsIgnoreCase(TRUE)及&安培;!obj.getString(用户id)equalsIgnoreCase()){
                    //调用下一页
                     adb.setMessage(登录成功......!);                     意向意图=新意图(这一点,HomeActivity.class); ** //这条线显示错误**
                     startActivity(意向);                }其他{//登录不成功
                    adb.setMessage(登录失败... !!);                }                adb.show();
            }赶上(JSONException E){
                Log.e(log_tag,JSONException+ e.toString());
            }
        }
        @覆盖
        在preExecute保护无效(){            TextView的loginStatus =(的TextView)findViewById(R.id.login);
            TextView的用户id =(的TextView)findViewById(R.id.userId);
            loginStatus.setText(未读取);
            userId.setText(未读取);        }        @覆盖
        保护无效onProgressUpdate(虚空......值){
        }
    }

寻找一个解决方案。先谢谢了。


解决方案

 意向意图=新意图(这一点,HomeActivity.class);

在上面的一行,这个的AsyncTask的实例意图使用应用程序或活动上下文作为构造函数的参数。

因此​​,它应该是这样的,

 意向意图=新意图(小于Your_Calling_Activity_Name.this>,HomeActivity.class);

在你的情况,

 意向意图=新意图(LoginActivity.this,HomeActivity.class);

 意向意图=新意图(mContext,HomeActivity.class);

下面 mContext 是呼吁的AsyncTask A活动上下文。你可以通过这方面的AsyncTask的构造函数。

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.

解决方案

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.

这篇关于Android的意图中的AsyncTask类给错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆