gpt4 book ai didi

java - 当单击按钮时,我调用了一个构造函数,在 android 中显示类转换异常

转载 作者:行者123 更新时间:2023-12-01 13:29:42 25 4
gpt4 key购买 nike

我使用此构造函数在单击按钮时创建数据库,当我运行它时,它将调用构造函数显示 java.lang.ClasscastException 错误,无论我在 onClickListener< 中进行类型转换 是否正确请大家提出建议

private static Context onClickListene;

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

这是构造函数的调用程序

                             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 不是 Context 的子类,因此如果您尝试强制转换它将给您一个 ClassCastException

您应该使用 ContextActivity 对象。

如果您在Activity中,请尝试使用YourActivityName.this

关于java - 当单击按钮时,我调用了一个构造函数,在 android 中显示类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com