简体   繁体   中英

When the button clicked i called a constructor shows class cast exception in android

I used this constructor to create a database when a button was clicked this will be call the constructor when i run it show java.lang.ClasscastException error whether my typecast in an onClickListener is correct or not please anyone suggest

private static Context onClickListene;

public DatabaseHandler3(OnClickListener onClickListene) 
{
    super((Context) onClickListene,DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

This is the calling program to a constructor

                             button2.setOnClickListener(new OnClickListener()
     {
      public void onClick(View v)
      {
              try
              {
                  final EditText edittxt4 = (EditText)findViewById(R.id.editText4);
                  final EditText edittxt5 = (EditText)findViewById(R.id.editText5);
                  final EditText edittxt6 = (EditText)findViewById(R.id.editText6);

                  String name=edittxt4.getText().toString();
                   String mailfamfri=edittxt5.getText().toString();
                   String phonefamfri=edittxt6.getText().toString();

                 int famatpos=mailfamfri.indexOf("@");
                   int famdotpos=mailfamfri.lastIndexOf(".");

                  int phonecount=phonefamfri.length();

                if(name.equals(""))
                 {
                     Toast.makeText(FiveActivity.this, "You Family or Friends name is empty",Toast.LENGTH_LONG).show();
                 }
                else if(mailfamfri.equals(""))
                {
                 Toast.makeText(FiveActivity.this, "You Families or Friend's Mail-ID is Empty",Toast.LENGTH_LONG).show();
                }
                else if(famatpos<1 || famdotpos<famatpos+2 || famdotpos+2>=mailfamfri.length())
                {
                Toast.makeText(FiveActivity.this, "You Families or Friend's Email-ID is not valid",Toast.LENGTH_LONG).show(); 
                }
                else if(phonefamfri.equals(""))
                 {
                 Toast.makeText(FiveActivity.this, "Phone Number is empty",Toast.LENGTH_LONG).show();
                }
               else if(phonefamfri.contains("[a-zA-z]"))
                {
                Toast.makeText(FiveActivity.this, "Phone Number should not contain any letters ",Toast.LENGTH_LONG).show();  
                }
               else if(phonecount>10 || phonecount<10)
               {
                  Toast.makeText(FiveActivity.this, "Phone Number Should Contain only 10 Numbers",Toast.LENGTH_LONG).show();  
               }

               else
               {
                String phoneno="+91"+phonefamfri; 


             // Toast.makeText(FiveActivity.this, "Good"+phoneno,Toast.LENGTH_LONG).show(); 
                DatabaseHandler3 db = new DatabaseHandler3(this);

                //Toast.makeText(FiveActivity.this,name+mailfamfri+phoneno,Toast.LENGTH_LONG).show(); 

                 Log.d("Insert: ", "Inserting .."); 
                 Log.d("Insert: ", "Inserting .."); 

OnClickListener is not a subclass of Context so if you try to cast it will give you an ClassCastException .

You should use either a Context or Activity object.

If you are in an Activity try using YourActivityName.this

it should be like this

 public DatabaseHandler3(Context context) {
        super(context,DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

call like this

DatabaseHandler3 db=new DatabaseHandler3(YourActivity.this);

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