此代码有什么问题?我的应用程序因点击并按住按钮而崩溃
activity_main.xml 如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Click and hold on this button" />
</RelativeLayout>
menu_main.xml ,如下所示:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu1"
android:icon="@drawable/ic_search_icon"
android:orderInCategory="100"
app:showAsAction="always"
android:title="Item 1"/>
<item
android:id="@+id/menu2"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:title="Item 2">
<menu>
<item
android:id="@+id/menu21"
android:title="Item 2.1"/>
<item
android:id="@+id/menu22"
android:title="Item 2.2"/>
<item
android:id="@+id/menu23"
android:title="Item 2.3"/>
</menu>
</item>
<item
android:id="@+id/menu3"
android:orderInCategory="100"
app:showAsAction="never"
android:title="Item 3"/>
<item
android:id="@+id/menu4"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:title="Item 4">
<menu>
<group
android:id="@+id/group"
android:checkableBehavior="single" >
<item
android:id="@+id/group_item1"
android:onClick="onGroupItemClick"
android:title="Red"/>
<item
android:id="@+id/group_item2"
android:onClick="onGroupItemClick"
android:title="Green"/>
<item
android:id="@+id/group_item3"
android:onClick="onGroupItemClick"
android:title="Blue"/>
</group>
</menu>
</item>
</menu>
和 MainActivity.java 如下所示: 包com.intents.dedan.contextandoptionmenus;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnCreateContextMenuListener(this);
}
//---Options Menu---
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onGroupItemClick(MenuItem item) {
if (item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
return true;
}
//---Context Menu---
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.menu_main, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
return true;
}
}
选项菜单似乎工作正常,但上下文菜单无效。