gpt4 book ai didi

java - android.support.v7.internal.view.menu.ActionMenuItemView 无法转换为 android.widget.Button

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

在我的应用程序中,我想调用一个新的Intent,但它显示异常:

java.lang.ClassCastException: android.support.v7.internal.view.menu.ActionMenuItemView cannot be cast to android.widget.Button I was find in the internet but can't find correct in my case.

我的问题始于HistoryFragment.java扩展Fragment来调用菜单项:

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.saveContact:
Button button2=(Button) getActivity().findViewById(R.id.saveContact);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), AddContactActivity.class);
startActivity(intent);
}
});

类:AddContactActivity.java extends AppCompatActivity 包含插入所有信息并添加到手机中的联系人的所有代码。

public class AddContactActivity extends AppCompatActivity {
private EditText editTextName;
private EditText editTextNumber;
private Button addContactBtn;
private CircleImageView contactImage;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);

editTextName = (EditText) findViewById(R.id.edtTxtName);
editTextNumber = (EditText) findViewById(R.id.edtTxtNumber);
addContactBtn = (Button) findViewById(R.id.addContactBtn);
contactImage = (CircleImageView) findViewById(R.id.contactImage);

final String nameStr = getIntent().getStringExtra("name");
final String numberStr = getIntent().getStringExtra("number");
final String idStr = getIntent().getStringExtra("id");

if(nameStr != null && numberStr != null && idStr != null) {
editTextName.setText(nameStr);
editTextNumber.setText(numberStr);
addContactBtn.setText("UPDATE");

Bitmap bitmap = getIntent().getParcelableExtra("image");
if(bitmap != null) {
contactImage.setImageBitmap(bitmap);
}
} else {
addContactBtn.setText("ADD");
}

addContactBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString().trim();
String number = editTextNumber.getText().toString().trim();

if(nameStr == null || numberStr == null || idStr == null) {
if (insertContact(name, number) && !(name.isEmpty() || number.isEmpty())) {
Toast.makeText(getApplicationContext(), "Đã thêm liên lạc", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Lỗi không thể thêm liên lạc", Toast.LENGTH_LONG).show();
}
} else {
if(updateContact(idStr, name, number)) {
Toast.makeText(getApplicationContext(), "Cập nhật 1 liên lạc", Toast.LENGTH_LONG).show();
//snackBar(v, "Contact updated.");
} else {
Toast.makeText(getApplicationContext(), "Lỗi không thể cập nhật liên lạc", Toast.LENGTH_LONG).show();
}
}
editTextName.setText("");
editTextNumber.setText("");
}
});
}

// Insert contact
public boolean insertContact(String firstName, String mobileNumber) {
ContentResolver contentResolver = this.getContentResolver();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, firstName).build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobileNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
return false;
}
return true;
}


boolean updateContact(String id, String name, String number) {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?", new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE})
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());

ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
+ "=? AND " + ContactsContract.CommonDataKinds.Organization.TYPE + "=?"
, new String[]{id, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)})
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch(Exception e) {
e.printStackTrace();
return false;
}
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}

最佳答案

在您的onActionItemClicked()中您不应将 R.id.saveContact 类型转换为 Button。您可以将其视为 View 类型直接使用。

View button2= getActivity().findViewById(R.id.saveContact);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), AddContactActivity.class);
startActivity(intent);
}
});

关于java - android.support.v7.internal.view.menu.ActionMenuItemView 无法转换为 android.widget.Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36165496/

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