Sohaib Jamal
Sohaib Jamal

Reputation: 111

android - to go from a menu option to a ListActivity

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 :)

Upvotes: 0

Views: 465

Answers (3)

Zulfiqar Chandio
Zulfiqar Chandio

Reputation: 263

When you call activity in onOptionsItemSelected, replace "this" with getApplicationContext().

Upvotes: 0

pouyan
pouyan

Reputation: 3439

comment all thing that you have write in your OnCreate of Select class(the thing's that comes after setcontentview() ) and check if still there is an error or not ?

Upvotes: 0

Mark Pazon
Mark Pazon

Reputation: 6205

Looking at your onCreateOptionsMenu() I am not sure if you have successfully created the menu. You can do this in two ways.

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

The other way is through a menu xml resource. Find more info here

Upvotes: 1

Related Questions