getApplicationContext()方法无法解析,LoginActivity.this不是封闭类

时间:2016-03-27 13:18:43

标签: android

在ProgressDialog初始化中,它显示LoginActivity.this不是封闭类。 在onPostExecute()方法中,Toast没有被执行,并且无法解析getApplicationContext()方法。

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

1 个答案:

答案 0 :(得分: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
}

快乐的编码;)