问题描述:

i have created a project with a menu (not empty project) that should open an list of my activities (and it does that) but when i want to open one of the activities that is already shown it doest work. It doesnt open the specified activity but instead it opens the main activity and so on, how can i link them up ? . Thanks.

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.nav_calendar) {

Intent i=new Intent(this, Calendar.class);

startActivity(i);

}

if (id == R.id.nav_info) {

Intent i=new Intent(this, CarInfo.class);

startActivity(i);

}

if (id == R.id.nav_expenses) {

Intent i=new Intent(this, Expenses.class);

startActivity(i);

}

if (id == R.id.nav_map) {

Intent i=new Intent(this, Map.class);

startActivity(i);

}

if (id == R.id.nav_service) {

Intent i=new Intent(this, Service.class);

startActivity(i);

}

return super.onOptionsItemSelected(item);

}

That's how i link every of my activity to menu.

I tried this too still doesnt do what i like:

I tried to link them like that :

public void CarInfo(View view){

Intent intent=new Intent(MainActivity.this, CarInfo.class);

startActivity(intent);

}

the problem i think it might be in the manifest:

 <application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name=".Calendar"></activity>

<activity android:name=".CarInfo"></activity>

<activity android:name=".Expenses"></activity>

<activity android:name=".History"></activity>

<activity android:name=".Map"></activity>

<activity android:name=".Service"></activity>

</application>

If i link only 1 activity its works, but how can I link them all ?

网友答案:

You should return true in the onOptionsItemSelected() when you use the event and leave the super.onOptionsItemSelected(item); when it's not handled.

网友答案:

Create Menu Item in XML

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/bookmark_menu" android:title="@string/bookmark" android:showAsAction="ifRoom"/> </menu>

Override onCreateOptionsMenu and use getMenuInflater().inflate

public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; }

Override onOptionsItemSelected(MenuItem item) to Handle Click Event

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.bookmark_menu: Toast.makeText(this, "You have selected Bookmark Menu", Toast.LENGTH_SHORT).show(); return true; } }

网友答案:

this is working for me... hope it will work for u

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent intent = new Intent(this, Wishlist.class);
        startActivity(intent);
    } else if (id == R.id.action_settings1) {
        Intent intent = new Intent(this, AboutUs.class);
        startActivity(intent);
    } else if (id == R.id.action_settings2) {
        Intent intent = new Intent(this, Contact_Us.class);
        startActivity(intent);
    } else if (id == R.id.action_cart) {
        Intent intent = new Intent(this, Cart.class);
        startActivity(intent);
    } else if (id == R.id.action_search) {
        Intent intent = new Intent(this, ActivitySearch.class);
        startActivity(intent);
    }

    return super.onOptionsItemSelected(item);

}

need to use else-if

相关阅读:
Top