android.support.v7.internal.view.menu.ActionMenuItemView无法强制转换为android.widget.Button

时间:2016-03-22 21:22:18

标签: java android casting

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

  

java.lang.ClassCastException:android.support.v7.internal.view.menu.ActionMenuItemView无法强制转换为android.widget.Button   我在互联网上找到了但在我的案例中找不到正确的内容。

我的问题从HistoryFragment.java extends 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);
    }
}

1 个答案:

答案 0 :(得分:1)

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);
                }
            });