2016-03-27 8 views
0

ProgressDialogの初期化では、LoginActivity.thisが囲みクラスではないことが示されています。 onPostExecute()メソッドでトーストが実行されず、getApplicationContext()メソッドを解決できません。getApplicationContext()メソッドを解決できません。LoginActivity.thisは囲みクラスではありません

public class LoginActivity extends AppCompatActivity { 

EditText et1,et2; 
Button b; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login2); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    et1=(EditText)findViewById(R.id.editText5); 
    et2=(EditText)findViewById(R.id.editText6); 
    b=(Button)findViewById(R.id.button2); 
} 
public void login(View view) 
{ 
    new BackgroundTask().execute(et1.getText().toString(),et2.getText().toString()); 
    Toast.makeText(this,"Button",Toast.LENGTH_LONG).show(); 
} 

} 

class Background extends AsyncTask<String, Integer, Integer> 
{ 
    ProgressDialog progressDialog=new ProgressDialog(LoginActivity.this);; 
    @Override 
    protected void onPreExecute() { 
    super.onPreExecute(); 
    progressDialog.setMessage("Processing"); 
    progressDialog.setTitle("Title"); 
    progressDialog.show(); 
} 

int check_point =0; 
String resp=""; 
@Override 
protected Integer doInBackground(String... params) { 
    try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost("http://192.168.1.6/login.php"); 
     HttpResponse response = client.execute(post); 
     InputStream in = response.getEntity().getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     String s; 
     while ((s = reader.readLine()) != null) { 
      resp=resp+s; 
      } 
    } 
    catch (Exception e) 
    { 
     System.out.print(e); 
    } 

    return check_point; 
} 

@Override 
protected void onPostExecute(Integer integer) { 
    super.onPostExecute(integer); 
    progressDialog.dismiss(); 
    Toast.makeText(getApplicationContext(),"ACtivity",Toast.LENGTH_LONG).show(); 
} 
} 
+0

は、どのようにログイン(ビュービュー)実際のボタンインスタンスに関連付けられていますか? Toasts&System.out.printの代わりにLogを使用してください。 onPreExecuteからprogressDialogを初期化するのはなぜですか? AsyncTaskがLoginActivityの内部クラスであることを考慮して、なぜアクティビティコンテキストでprogressDialogを初期化し、アプリケーションコンテキストonPostExecuteを使用しますか? – randomuser

+0

私は何をしていますか? –

+0

"LoginActivity.this"の代わりにキーワード "this"を使用すると、問題が解決するはずです –

答えて

1

クラスBackgroundは内部クラスではありません。それはLoginActivityの範囲外です。 なぜそのエラーが発生しているのですか?内部クラスとして移動してみてください。

classs ParentClass{ 
    //some fields, methods whatevenr 

    class InnerClass{ 
     // this is inner 
    } 
} 

class OutSideScope{ 
    // this is outside of the scope of the Parent Class 
} 

ハッピーコーディング ;)

+0

ああ、ありがとうございます。私はそれを認識せず、なぜ私のコードがうまくいかないのか心配し続けました。ありがとう。 –

関連する問題