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

时间:2016-03-14 17:11:36

标签: android android-studio button android-widget menuitem

我的Activity中的

是一个填充数据库的按钮。我的方式是一个动作棒项目,它做同样的事情。我不明白我的问题是什么。 在ActionBar中使用Button

的saveItem的最佳方法是什么
public class ItemActivity extends AppCompatActivity {

public static final String LOG_TAG = ItemActivity.class.getSimpleName();

private
DeviceMemoSource dataSource;
Button action_save;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item);
    action_save = (Button) findViewById(R.id.action_save);


    Log.d(LOG_TAG, "Das Datenquellen-Objekt wird angelegt.");
    dataSource = new DeviceMemoSource(this);


}

private void activateAddButton() {

    Button buttonAddProduct = (Button) findViewById(R.id.action_save);
    final EditText editText_Device_Number = (EditText) findViewById(R.id.editText_device_number);
    final EditText editText_Device_Typ = (EditText) findViewById(R.id.editText_device_typ);
    final EditText editText_Device_Date = (EditText) findViewById(R.id.editText_device_date);
    final EditText editText_Device_Hits = (EditText) findViewById(R.id.editText_device_hits);
    final EditText editText_Device_category = (EditText) findViewById(R.id.editText_device_category);

    buttonAddProduct.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            String numberString = editText_Device_Number.getText().toString();
            String typ = editText_Device_Typ.getText().toString();
            String date = editText_Device_Date.getText().toString();
            String hits = editText_Device_Hits.getText().toString();
            String category = editText_Device_category.getText().toString();


            int number = Integer.parseInt(numberString);
            editText_Device_Number.setText("");
            editText_Device_Typ.setText("");
            editText_Device_Date.setText("");
            editText_Device_Hits.setText("");
            editText_Device_category.setText("");

            dataSource.createDeviceMemo(typ, number, date, hits, category);

            InputMethodManager inputMethodManager;
            inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            if (getCurrentFocus() != null) {
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }

            showAllListEntries();
        }
    });
}


private void showAllListEntries() {
    List<DeviceMemo> deviceMemoList = dataSource.getAllDeviceMemos();

    ArrayAdapter<DeviceMemo> deviceMemoArrayAdapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_multiple_choice,
            deviceMemoList);

    ListView deviceMemosListView = (ListView) findViewById(R.id.listview_device_memos);
    deviceMemosListView.setAdapter(deviceMemoArrayAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_item, menu);
    return true;
}


@Override
protected void onResume() {
    super.onResume();

    Log.d(LOG_TAG, "Die Datenquelle wird geöffnet.");
    dataSource.open();


    Log.d(LOG_TAG, "Folgende Einträge sind in der Datenbank vorhanden:");
    showAllListEntries();

}

@Override
protected void onPause() {
    super.onPause();

    Log.d(LOG_TAG, "Die Datenquelle wird geschlossen.");
    dataSource.close();
}


public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_save) {
        activateAddButton();
        return true;
    } else if (id == R.id.about) {
        Toast.makeText(getApplicationContext(), "aboutText", Toast.LENGTH_LONG).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public void onStart() {
    super.onStart();

}

@Override
public void onStop() {
    super.onStop();

}

}

和xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_save"
    android:orderInCategory="100"
    android:title="@string/save"
    android:icon="@android:drawable/ic_menu_save"
    app:actionViewClass="android.widget.button"
    app:showAsAction="always"/>

<item
    android:id="@+id/about"
    android:orderInCategory="100"
    android:title="@string/about"
    app:showAsAction="never"/>

1 个答案:

答案 0 :(得分:4)

您可以使用Button而不是将其声明并强制转换为View 您还可以将onClickListener添加到视图中。 就这样做:

View action_save = findViewById(R.id.action_save);

View buttonAddProduct = findViewById(R.id.action_save);

这可能不是最好的解决方案,但它应该有用,我不知道你的action_save代表什么。