2016-12-04 35 views
0

我已經創建了一個應該打開我的活動列表(而不是空項目)的項目,但是當我想打開其中一個已經顯示它的活動正在發揮作用。它不打開指定的活動,而是打開主要活動等等,我怎樣才能將它們聯繫起來? 。謝謝。如何將活動鏈接到菜單並訪問它們

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

這就是我的每一個鏈接我的活動菜單。 我想這也仍然沒有做什麼,我想: 我想他們像鏈接:

public void CarInfo(View view){ 
Intent intent=new Intent(MainActivity.this, CarInfo.class); 
startActivity(intent); 

}

我想這可能是清單中的問題:

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

如果我只鏈接1項活動的作品,但我怎樣才能鏈接它們呢?

+0

預期的'Activity'應該只是開始精細。此外,這段代碼似乎是正確的。請分享您的應用和/或錯誤/調試日誌中的任何其他相關代碼。 –

+0

我認爲問題可能在這裏,在清單中: – CobianuA

回答

0

當您使用該事件時,您應該在onOptionsItemSelected()中返回true,並在未處理時保留super.onOptionsItemSelected(item);

0

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

覆蓋onCreateOptionsMenu創建菜單項,並使用getMenuInflater()。充氣

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

覆蓋onOptionsItemSelected(菜單項的項目)來處理點擊事件

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

+1

非常感謝你 – CobianuA

0

,這是爲我工作...希望它會工作的ü

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

} 

需要使用其他,如果

+0

是的,非常感謝! – CobianuA

+0

@CobianuA請讓它有用的答案..如果你工作.. –

相關問題