Question

Hello my friend) I have next error in my class...

public class AlertDlg {
public static void AlertShow(final Activity activity, EditText name, EditText track)
{
    LayoutInflater inflater = activity.getLayoutInflater();
    View alertLayout = inflater.inflate(R.layout.layout_custom_dialog, null);
    name = (EditText) alertLayout.findViewById(R.id.et_name);
    track = (EditText) alertLayout.findViewById(R.id.et_track);
    AlertDialog.Builder alert = new AlertDialog.Builder(activity);
    alert.setTitle("Add track number");
    //alert.setIcon(R.drawable.plus);
    alert.setView(alertLayout);
    alert.setCancelable(false);
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(activity.getBaseContext(), "Abort", Toast.LENGTH_SHORT).show();
        }
    });
    alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
    ERROR HERE--->String sname = name.getText().toString();
            //String track = etPassword.getText().toString();
            Toast.makeText(activity.getBaseContext(), "Name: " + sname +"  "+  " Track: " , Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog dialog = alert.create();
    dialog.show();
}}

Error: Cannot refer to a non-final variable name inside an inner class defined in a different method. I'm trying to get data from the EditText. But get this error ...

calling a method :

AlertDlg.AlertShow(MainActivity.this,name,track);
Was it helpful?

Solution

Remove the inner class, use it just as a function inside your activity.

public void AlertShow()
{
    Context mContext = this;
    LayoutInflater inflater = getLayoutInflater();
    View alertLayout = inflater.inflate(R.layout.layout_custom_dialog, null);
    final EditText name = (EditText) alertLayout.findViewById(R.id.et_name);
    //final EditText track = (EditText) alertLayout.findViewById(R.id.et_track);
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Add track number");
    //alert.setIcon(R.drawable.plus);
    alert.setView(alertLayout);
    alert.setCancelable(false);
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(mContext, "Abort", Toast.LENGTH_SHORT).show();
        }
    });
    alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            String sname = name.getText().toString();
            //String track = etPassword.getText().toString();
            Toast.makeText(mContext, "Name: " + sname +"  "+  " Track: " , Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog dialog = alert.create();
    dialog.show();
}

Then to call it:

AlertShow();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top