我的操作栏中有一个按钮,我希望当用户点击该按钮时,它会显示另一个活动,例如普通按钮的意图。这是我的主要活动:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.item, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case R.id.exit:
new AlertDialog.Builder(MainActivity.this)
.setMessage("میخوای از برنامه بری بیرون؟")
.setCancelable(true)
.setPositiveButton("اره :(",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
}
}).setNegativeButton("نه :)", null).show();
case R.id.info:
}
return true; }}
我想要那个案例的意图R.id.info:我试过这段代码:
case R.id.info:
public void on click(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,Info.class);
start activity(intent);
}
但它不起作用并且显示一些错误
请某人帮助我:(答案 0 :(得分:2)
在manifest.xml中注册您的活动
类名应该在android:name
中<activity
android:name=".ToActivity"
android:label="@string/app_name">
</activity>
答案 1 :(得分:0)
将您的onOptionsItemSelected(MenuItem item)
更改为:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case case R.id.exit:
new AlertDialog.Builder(MainActivity.this)
.setMessage("میخوای از برنامه بری بیرون؟")
.setCancelable(true)
.setPositiveButton("اره :(",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
finish();
}
}).setNegativeButton("نه :)", null).show();
break;
case R.id.info:
Intent intent = new Intent(MainActivity.this, Info.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
答案 2 :(得分:0)
Use this, hope it works for you:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
mainMenu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.info) {
startActivity(new Intent(getApplicationContext(), Info.class));
return true;
}
return super.onOptionsItemSelected(item);
}