1

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

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

问题:按照stackoverflow用户的建议,我已经使用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) {


        }
    }

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

4

4 回答 4

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

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

所以应该是这样的,

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 的构造函数。

于 2013-02-14T06:45:07.563 回答
1

尝试这个

  Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
  startActivity(intent);
于 2013-02-14T06:48:17.173 回答
0

问题就在这里——

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

在上面的代码中 -指的是 AsyncTask 类。

相反,您可以使用LoginActivity.this或 context 。

Intent intent = new Intent(LoginActivity.this,HomeActivity.class);
于 2013-02-14T06:47:55.940 回答
0

尝试这个

startActivity(new Intent(LoginActivity.this, HomeActivity.class));
于 2017-11-23T13:20:13.577 回答