android.support.v7.view.menu.ActionMenuItemView 无法转换为 android.widget.Button
Posted
技术标签:
【中文标题】android.support.v7.view.menu.ActionMenuItemView 无法转换为 android.widget.Button【英文标题】:android.support.v7.view.menu.ActionMenuItemView cannot be cast to android.widget.Button 【发布时间】:2016-06-29 20:46:21 【问题描述】:在我的活动中是一个填充数据库的按钮。我的方式是做同样的事情的 actionBar 项目。我不明白我的问题是什么。 在没有按钮的情况下,在 ActionBar 中保存项目的最佳方式是什么
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】:您可以使用View
,而不是将其声明和类型转换为Button
您还可以将 onClickListener 添加到视图中。
只需这样做:
View action_save = findViewById(R.id.action_save);
和
View buttonAddProduct = findViewById(R.id.action_save);
这可能不是最好的解决方案,但它应该可以工作,我不知道你的 action_save
代表什么。
【讨论】:
是的可以关闭问题谢谢以上是关于android.support.v7.view.menu.ActionMenuItemView 无法转换为 android.widget.Button的主要内容,如果未能解决你的问题,请参考以下文章