简体   繁体   中英

Cannot resolve symbol AlertDialogActivity

So I'm just trying to create an Alert Dialog that is just a message (no buttons or titles). I want to display an alert dialog when a background task is running. The alert dialog will run on the UI thread.

Here's what I have done so far:

protected void onPreExecute() {
        super.onPreExecute();

        AlertDialog altDlg;
        altDlg = new AlertDialog.Builder(AlertDialogActivity.this).create();

        altDlg.setMessage("Retrieving Information. Please Wait");

        altDlg.show();

    }

I also tried doing this:

 AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setMessage("Retrieve Info. Please Wait").show();

The error I am getting with the first one is:

  cannot find symbol 'AlertDialogActivity'
  symbol:   class AlertDialogActivity
  location: class com.example.Device.Activity

The second attempt error says:

 incompatible types: com.example.Device.Activity cannot be converted to android.content.Context

I'm not sure what I am doing wrong in either scenario. I just want to display a basic message when a background task is running and I was hoping the closest thing I can use is AlertDialog.

EDIT for how to set up AsyncTask properly:

Small background of what I want to do. I just want to read in a file, deserialize it and save it's contents to a db.

Right now I'm assuming I only need two activities.

One is my main activity:

public class MainActivity extends Activity {

 /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setup);

   final Button setup_button = (Button) findViewById(R.id.setup_button);
   setup_button.setOnClickListener (new View.OnClickListener() {
       public void onClick(View view){
           setContentView(R.layout.retrieve_info);
    }
  });
}
}

Now the onClick event just moves to the new view that is supposed to display the message or alert dialog that says retrieving information. Please Wait. It displays the message while reading a file and saving to db. Once the file is read and saved, The message should disappear and say something like setup complete.

My second activity so far is:

public class RetrieveInfoActivity extends AsyncTask<Void,Void,Void>  {


    private ProgressDialog progressBar;

    private void retrieveInfo(String fileName) {

        try {

            File file = new File(fileName);
            Scanner scanner = new Scanner(file);

            //Read all the lines until there are no more lines
            while (scanner.hasNextLine()) {

                scanner.nextLine();

            //TODO: deserialize and save to db

            }
            scanner.close();
        }

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

    @Override
    protected Void doInBackground(Void... params) {
        retrieveInfo("test.txt"); 
        return null;
    }

    protected void onPreExecute() {
        super.onPreExecute();

        progressBar.setIndeterminate(true);
        progressBar.setCancelable(false);
        progressBar.setMessage("Retrieve Information.Please wait");
        progressBar.show();

    }

    @Override
    protected void onPostExecute() {

        progressBar.dismiss();
    }

}

That's all I really have so far. I just need to understand how to set up this in Android conceptually.

Hope this makes sense.

尝试这个:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

Instead of using an AlertDialog use a ProgressBar, it will do the trick for you.

private ProgressDialog progressBar;

@Override
    protected void onPreExecute() {

        super.onPreExecute();

        progressBar.setIndeterminate(true);
        progressBar.setCancelable(false);
        progressBar.setMessage("Your message");
        progressBar.show(); 
}

@Override
    protected void onPostExecute(final String error_code) {

            progressBar.dismiss();
}

Looks like you are extending AsyncTask and trying to use it as a context. That won't work as AsyncTask itself is nothing but an abstract class.

You need to create a custom constructor for your AsyncTask to fetch the Context:

public class MyTask extends AsyncTask<Void, Void, Void> {

    private Context mCtx;

    public MyTask(Context context) {
        mCtx = context;
    }
    ...

Then when starting your AsyncTask, pass the context:

new MyTask(this).execute();

Another way would be to make the AsyncTask an inner class and use YourActivity.this when creating the dialog. Example:

public class YourActivity extends Activity {

    ...

    private class MyTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            AlertDialog dialog = new AlertDialog.Builder(YourActivity.this).create();
        }

        @Override
        protected Void doInBackground(Void... params) {
            ...
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM