2014-12-01 56 views
0

我在應用程序中出現了一些關於物理菜單按鈕的奇怪,不一致的行爲。物理菜單/溢出按鈕在某些視圖中無響應

以下可能會有幫助或可能沒有幫助: 我有一個應用程序,根據設備+屏幕大小+方向更改其UI的應用程序。在橫向平板電腦上,我使用同時查看2個片段的活動(聯繫人列表+所選聯繫人的聊天)。在手機上,在平板電腦上縱向擺放,我改用兩項獨立的活動;一個用於聯繫人列表,一個用於聊天。

我提到這一點是因爲我觀察到以下奇怪行爲:手機和平板電腦縱向方向上,我設備上的物理菜單按鈕在我的應用程序中沒有反應。當平板電腦處於橫向模式時,物理按鈕按預期工作(顯示我的操作欄的溢出菜單)。菜單按鈕在我的應用程序外正常工作。

我原本以爲問題出在我手機的android版本上,但考慮到我在縱向上也看到了平板電腦的問題,我不確定問題出在哪裏。我沒有硬編碼物理菜單按鈕的任何代碼,所以它不應該是一些功能不適合手機和麪向肖像的平板電腦的單窗格視圖複製。

任何想法或答案將不勝感激! 謝謝

編輯:添加了要求的代碼。

的縱向活動:

package com.h.r.android.tcip; 

import android.content.ComponentName; 
import android.content.ContentUris; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 
import android.database.ContentObserver; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.IBinder; 
import android.support.v4.app.Fragment; 
import android.util.Log; 

import com.actionbarsherlock.view.Menu; 
import com.actionbarsherlock.view.MenuItem; 
import com.h.r.android.tctip.ConversationFragment.Builder; 
import com.h.r.android.tctip.data.TacMessage; 
import com.h.r.android.tctip.db.AddressmessageSchema; 
import com.h.r.android.tctip.settings.PreferencesHelper; 

public class ConversationActivity extends LoggedInTCActivity { 

    @SuppressWarnings("unused") 
    private final String TAG = ConversationActivity.class.getSimpleName(); 
    /** 
    * Projection for address database query results. 
    */ 
    protected static final String[] ADDRESS_PROJECTION = new String[] { 
      AddressmessageSchema.UID, AddressmessageSchema.HANDLE }; 

    /** 
    * The TCService running in the background doing the send/receive work. 
    */ 
    protected TCService mService; 

    /** 
    * Handler used for the partner handle cursor. 
    */ 
    protected final Handler mHandler = new Handler(); 

    /** 
    * Used to track partner handle changes. 
    */ 
    protected ContentObserver mHandleObserver = new ContentObserver(mHandler) { 
     /* 
     * (non-Javadoc) 
     * 
     * @see android.database.ContentObserver#onChange(boolean) 
     */ 
     @Override 
     public void onChange(boolean selfChange) { 
      super.onChange(selfChange); 

      getSupportActionBar().setTitle(getPartnerHandle()); 
     } 
    }; 

    /** 
    * The actual partner ID, from the intent. 
    */ 
    static long mPartnerId; 

    /** 
    * The connection binding us to the Service. We're using this to 
    * indicate to Android the dependency between this Activity and the running 
    * Service. 
    */ 
    protected final ServiceConnection mConnection = new ServiceConnection() { 
     /* 
     * (non-Javadoc) 
     * 
     * @see 
     * android.content.ServiceConnection#onServiceDisconnected(android.content 
     * .ComponentName) 
     */ 
     public void onServiceDisconnected(ComponentName name) { 
      mService = null; 
     } 

     /* 
     * (non-Javadoc) 
     * 
     * @see 
     * android.content.ServiceConnection#onServiceConnected(android.content 
     * .ComponentName, android.os.IBinder) 
     */ 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      mService = ((TCService.TCServiceBinder) service) 
        .getService(); 
     } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setTheme(PreferencesHelper.getThemeId(this)); 

     ConversationFragment frag = new ConversationFragment.Builder(
       getIntent().getExtras()).build(); 


     setContentView(R.layout.tc_conv); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     /* 
     if (savedInstanceState != null) 
     { 
      mPartnerId = savedInstanceState.getLong("mPartnerId", mPartnerId); 
     } 
     else{ 
     */ 
     // The Fragment's partnerId 
     mPartnerId = frag.getPartnerId();   
     //} 


     getSupportActionBar() 
       .setTitle(
         mPartnerId == 0 ? getString(R.string.tc_all_users_chat) 
           : getPartnerHandle()); 

     getSupportFragmentManager().beginTransaction() 
       .add(R.id.conversation, frag).commit(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     startService(new Intent(this, TCService.class)); 
     //startService(new Intent(this, DiscoveryService.class)); 

     // bind to the service to disable notifications while this is up 
     bindService(new Intent(this, TCService.class), mConnection, 0); 

     if (mPartnerId != 0) { 
      Uri handleUri = ContentUris.withAppendedId(TacMessage.ADDRESS_URI, 
        mPartnerId); 
      getContentResolver().registerContentObserver(handleUri, true, 
        mHandleObserver); 
      mHandleObserver.onChange(true); 
     } 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.support.v4.app.FragmentActivity#onPause() 
    */ 
    @Override 
    protected void onPause() { 
     if (mService != null) { 
      unbindService(mConnection); 
     } 

     if (mPartnerId != 0) { 
      getContentResolver().unregisterContentObserver(mHandleObserver); 
     } 

     super.onPause(); 
    } 

    /** 
    * Get the partner handle to show in the action bar. 
    * 
    * @return 
    */ 
    protected String getPartnerHandle() { 
     // Generic default in case something goes wrong. 
     String partnerHandle = getString(R.string.t_c_app_name); 

     Cursor c = null; 
     try { 
      String selection = AddressmessageSchema.UID + " = ?"; 
      String[] selectionArguments = { "" + mPartnerId }; 
      c = getContentResolver().query(TacMessage.ADDRESS_URI, 
        ADDRESS_PROJECTION, selection, selectionArguments, null); 
      if (c != null && c.moveToFirst()) { 
       partnerHandle = c.getString(c 
         .getColumnIndex(AddressmessageSchema.HANDLE)); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (c != null) { 
       c.close(); 
      } 
     } 

     return partnerHandle; 
    } 

    /* 
    * Can be called from screen orientation change or keyboard hidden. This is 
    * being used to prevent the Activity from being destroyed or rebuilt so 
    * that during zip and image conversion activities, we don't end up killing 
    * the app by rotating the screen and such. 
    * 
    * (non-Javadoc) 
    * 
    * @see 
    * android.support.v4.app.FragmentActivity#onConfigurationChanged(android 
    * .content.res.Configuration) 
    */ 
    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
     { 
      //Builder.CONTACT_UID = getString((int) mPartnerId); 
      Log.d("gabe", "the config is changing in convo activity" + mPartnerId); 
      //finish(); 

     } 
     // Pass in the orientation of sensor so that we keep receiving these 
     // calls. 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
    } 

    public void onDestroy(Bundle savedInstanceState) 
    { 
     Log.d("gabe", "the conver activity is being destroyed" + mPartnerId); 

    } 
    public void onSaveInstance(Bundle savedInstanceState) 
    { 
     Log.d("gabe", "the con act is c saving instance"); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // TODO Auto-generated method stub 
     getSupportMenuInflater().inflate(R.menu.chat_only_menu, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    /* 
    * Add the up (ancestral) navigation. 
    * 
    * (non-Javadoc) 
    * 
    * @see 
    * com.actionbarsherlock.app.SherlockFragmentActivity#onOptionsItemSelected 
    * (android.view.MenuItem) 
    */ 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      // Ancestral navigation (Home/Up button on Action Bar) 
      Intent parentActivityIntent = new Intent(this, 
        TCActivity.class); 
      // See the ancestral navigation docs about synthesizing a back 
      // stack, if we ever have need for more back steps than the 
      // TCActivity class. 
      // http://developer.android.com/training/implementing-navigation/ancestral.html 
      parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
        | Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(parentActivityIntent); 
      finish(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public boolean onContextItemSelected(android.view.MenuItem item) { 
     // TODO Auto-generated method stub 
     Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentById(R.id.conversation); 
     if(null != fragment) { 
      fragment.onContextItemSelected(item); 
     } 
     return super.onContextItemSelected(item); 
    } 
} 

的畫像活動的XML,你想可能沒有什麼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/conversation" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

聊天只XML

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

    <group 
     android:id="@+id/group_alert_checked" 
     android:checkableBehavior="all" > 
     <item 
      android:id="@+id/menu_alert" 
      android:checked="false" 
      android:icon="?attr/buttonface_alert_unchecked" 
      android:showAsAction="always" 
      android:title="@string/menu_set_alert_message_mode"/> 
    </group> 

    <item 
     android:id="@+id/menu_attach_existing_picture" 
     android:icon="?attr/buttonface_existing_picture" 
     android:showAsAction="always" 
     android:title="@string/menu_attach_existing_picture"> 
    </item> 
    <item 
     android:id="@+id/menu_attach_new_picture" 
     android:icon="?attr/buttonface_new_picture" 
     android:showAsAction="always" 
     android:title="@string/menu_attach_new_picture"> 
    </item> 
    <item 
     android:id="@+id/menu_attach_file" 
     android:icon="?attr/buttonface_attach_file" 
     android:showAsAction="always" 
     android:title="@string/menu_attach_file"> 
    </item> 
    <item 
     android:icon="?attr/buttonface_overflow" 
     android:showAsAction="always"> 
     <menu> 
      <item 
       android:id="@+id/menu_add" 
       android:showAsAction="ifRoom" 
       android:title="@string/menu_new_contact"> 
      </item> 
      <item 
       android:id="@+id/menu_clear_conversation" 
       android:showAsAction="ifRoom" 
       android:title="@string/menu_clear_conversation"> 
      </item> 
      <item 
       android:id="@+id/menu_save_conversation" 
       android:showAsAction="ifRoom" 
       android:title="@string/menu_save_conversation"> 
      </item> 
      <item 
       android:id="@+id/menu_logout" 
       android:showAsAction="ifRoom" 
       android:title="@string/logout"/> 
     </menu> 
    </item> 

</menu> 

我也有片段和片段xml的代碼,這也可能對你有用,但片段代碼非常大。我認識到,該活動的XML不是很有幫助,但沒有休息。讓我知道還有什麼可以幫助的,我會找出更好的分享方式。

+0

你可以發佈你的菜單xml嗎? – petey 2014-12-01 20:27:16

+0

,併發布在縱向定位活動中添加xml的代碼。 – petey 2014-12-01 20:31:41

+0

我添加了肖像取向活動代碼和它的xml。我沒有特定的menu.xml文件,每個活動和片段都有xml文件,我想他們會根據需要傳遞。對不起,如果它沒有幫助,讓我知道我還能提供什麼。 – gabe3vino 2014-12-02 16:30:06

回答

1

好吧,我認爲你需要編輯你的onOptionsItemSelected覆蓋來處理不同的菜單項,下面是一個使用if else而不是switch的例子,如使用switches has issues in library projects

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int itemId = item.getItemId(); 
    if (itemId == R.id.menu_alert) { 
     // do something here 
     return true; 
    } else if (itemId == R.id.menu_attach_existing_picture) { 
     // do something here 
     return true; 
    } else if (itemId == R.id.menu_attach_new_picture) { 
     // do something here 
     return true; 
    } else if (itemId == R.id.menu_attach_file) { 
     // do something here 
     return true; 
    } else if (itemId == R.id.menu_add) { 
     // do something here 
     return true; 
    } else if (itemId == R.id.menu_clear_conversation) { 
     // do something here 
     return true; 
    } else if (itemId == R.id.menu_save_conversation) { 
     // do something here 
     return true; 
    } else if (itemId == R.id.menu_logout) { 
     // do something here 
     return true; 
    } else if (itemId == android.R.id.home) { 
     // Ancestral navigation (Home/Up button on Action Bar) 
     Intent parentActivityIntent = new Intent(this, 
               TCActivity.class); 
     // See the ancestral navigation docs about synthesizing a back 
     // stack, if we ever have need for more back steps than the 
     // TCActivity class. 
     // http://developer.android.com/training/implementing-navigation/ancestral.html 
     parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
             | Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(parentActivityIntent); 
     finish(); 
     return true; 
    } else if (super.onOptionsItemSelected(item)) { 
     // parent activity handled this selection 
     return true; 
    } 
    return false; 
} 
+0

編輯:添加鏈接到ADT 14更改要求開發人員避免使用開關語句的資源ID – petey 2014-12-02 17:17:02

+0

謝謝!我會嘗試這個修復(午餐後),並會更新!非常感激! – gabe3vino 2014-12-02 17:31:40