java - 错误: is not an enclosing class

标签 java android mysql

我的项目有问题。 我有 2 个类:MainActivity 和 Insert。 我在“插入”类中出现错误。 对话框 = new ProgressDialog(MainActivity.this); 我的错误是“不是封闭类”。 请帮助我

代码插入:

public class Insert extends AsyncTask<String, Void, Boolean> {

ProgressDialog dialog;

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog = new ProgressDialog(MainActivity.this); //error is here
    dialog.setMessage("Inserting details, please wait");
    dialog.setTitle("Connecting... ");
    dialog.show();
    dialog.setCancelable(false);
}

@Override
protected Boolean doInBackground(String... urls) {


    try {

        List<NameValuePair> insert = new ArrayList<NameValuePair>();
        insert.add(new BasicNameValuePair("name", nameStr));
        insert.add(new BasicNameValuePair("country", countryStr));
        insert.add(new BasicNameValuePair("email", emailStr));
        insert.add(new BasicNameValuePair("hobbies", hobbiesStr));


        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(
                "http://bookcompr.netne.net/input.php");
        httpPost.setEntity(new UrlEncodedFormEntity(insert));

        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity entity = response.getEntity();

        return true;


    } catch (IOException e) {
        e.printStackTrace();

    }


    return false;
}

protected void onPostExecute(Boolean result) {
    dialog.cancel();

    AlertDialog.Builder ac = new AlertDialog.Builder(MainActivity.this);//error is here

    ac.setTitle("Result");
    ac.setMessage("Details Successfully Inserted");
    ac.setCancelable(true);

    ac.setPositiveButton(
            "Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();

                }
            });

    AlertDialog alert = ac.create();
    alert.show();
}

}

代码主要 Activity

public class MainActivity extends AppCompatActivity {

EditText name, country, email, hobbies;

Button insert;

String nameStr, countryStr, emailStr, hobbiesStr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    name = (EditText) findViewById(R.id.name);
    country = (EditText) findViewById(R.id.country);
    email = (EditText) findViewById(R.id.email);
    hobbies = (EditText) findViewById(R.id.hobbies);

    insert = (Button) findViewById(R.id.insert);

    insert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            nameStr = name.getText().toString();
            countryStr = country.getText().toString();
            emailStr = email.getText().toString();
            hobbiesStr = hobbies.getText().toString();

            new Insert().execute();
        }
    });
}

public class Insert extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Inserting details, please wait");
        dialog.setTitle("Connecting... ");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {


        try {

            List<NameValuePair> insert = new ArrayList<NameValuePair>();
            insert.add(new BasicNameValuePair("name", nameStr));
            insert.add(new BasicNameValuePair("country", countryStr));
            insert.add(new BasicNameValuePair("email", emailStr));
            insert.add(new BasicNameValuePair("hobbies", hobbiesStr));


            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(
                    "http://bookcompr.netne.net/input.php"); // link to connect to database
            httpPost.setEntity(new UrlEncodedFormEntity(insert));

            HttpResponse response = httpClient.execute(httpPost);

            HttpEntity entity = response.getEntity();

            return true;


        } catch (IOException e) {
            e.printStackTrace();

        }


        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();

        AlertDialog.Builder ac = new AlertDialog.Builder(MainActivity.this);
        ac.setTitle("Result");
        ac.setMessage("Details Successfully Inserted");
        ac.setCancelable(true);

        ac.setPositiveButton(
                "Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();

                    }
                });

        AlertDialog alert = ac.create();
        alert.show();
    }

}

}

最佳答案

在 AsyncTask 的构造函数中,您需要传递 Context 并在需要时使用它。

public class Insert extends AsyncTask<Void, Void, String> {
   private Context mContext;
   public Insert (Context context){
        mContext = context;
   }

   protected void  onPreExecute() {
      super.onPreExecute();
      ProgressDialog pd= new ProgressDialog(mContext); //changes is here
      pd.setMessage("your message");
      pd.show();
   }
}

在mainActivity中像这样使用它。

Insert insert = new Insert(getApplicationContext());
insert.execute();

关于java - 错误: is not an enclosing class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39476632/

相关文章:

mysql - 需要正确查询以根据条件获取唯一数据计数

mysql - 在数据库中查找两个名字作为全名列的一部分

java - 如何切换对象字段的数据类型?

android - 比较 SQLite 中的时间

Android Studio 缺少初始分区文件 :

android - 在动态添加的单选按钮中未正确设置布局权重

mysql - 通过索引优化查询

java - ARcore - 在不使用 TransformableNode 的情况下无法旋转模型

java - 两个线程之间的通信

java - 在 Spring Boot gradle 中配置嵌入式 tomcat 上下文 docBase 和路径