我是Android开发的新手,我需要从菜单选项到活动的帮助。 当我这样做时,应用程序崩溃了。我已经在清单文件中添加了活动,但它仍然无法正常工作。似乎无法找到问题所在。当我单击选项菜单中的选择但是日志工作正常时,应用程序崩溃。
在Tasb.java
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.select:
startActivity(new Intent(this, Select.class));
return true;
case R.id.log:
startActivity(new Intent(this, Log.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
select_tasb.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="20sp">
</ListView>
在Select.java中
public class Select extends Activity{
private ListView l;
static final String[] TASB = new String[] { "Tasbeeh-e-Fatima", "SubhanAllahi'l-adheem wa biHamdihi", "La Hawla wa la Quwatta illa Billah",
"La illaha ilAllah(u)", "SubhanAllah", "SubhanAllahi wa biHamdihi" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.select_tasb);
l = (ListView) findViewById(R.id.list);
l.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,TASB));
// ListView listView = getListView(); l.setTextFilterEnabled(真);
l.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),((TextView) view).getText(), Toast.LENGTH_SHORT).show();
Intent intent = null;
if(position == 0) {
intent = new Intent(Select.this,One.class);
} else if(position == 1) {
intent = new Intent(Select.this,One.class);
} else if(position == 2) {
intent = new Intent(Select.this,One.class);
}
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
}
One.java是一个简单的活动。提前谢谢你:)
答案 0 :(得分:1)
查看您的onCreateOptionsMenu()
我不确定您是否已成功创建菜单。你可以用两种方式做到这一点。
private static final int MENU_LAUNCH_ACTIVITY = 123; //you can set any id you like
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add(Menu.NONE, MENU_LAUNCH_ACTIVITY, Menu.NONE, "Launch Activity");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case MENU_LAUNCH_ACTIVITY:
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
break;
}
return true;
}
另一种方式是通过菜单xml资源。查找更多信息here
答案 1 :(得分:0)
评论你在Select OnCreate
中选择的所有内容(在setcontentview()之后发生的事情)并检查是否仍有错误?
答案 2 :(得分:0)
当你在onOptionsItemSelected中调用activity时,用getApplicationContext()替换“this”。