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

上記の行で、thisAsyncTaskのインスタンス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のコンストラクターに渡すことができます。

于 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 に答える