0

i am new to android development and i need help with going from a menu option to an activity. teh app crashes when i do this. Ive added the activity in the manifest file but it still doesnt work. cant seem to find the problem. The app crashes when i click on the select in the options menu but the log works fine.

In 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>

In 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(true);

    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);
}

}

the One.java is a simple activity. Thank you in advance :)

4

3 回答 3

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 资源。在此处查找更多信息

于 2013-07-01T10:27:47.327 回答
0

在 onOptionsItemSelected 中调用活动时,将“this”替换为 getApplicationContext()。

于 2013-07-01T12:37:38.967 回答
0

评论您OnCreate在 Select 类中编写的所有内容(在 setcontentview() 之后的内容)并检查是否仍然存在错误?

于 2013-07-01T10:32:13.453 回答