2013-01-17 98 views
0

我Activity類():動作條選項卡的內容不顯示

package com.wts.ui; 

import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.ActionBar.Tab; 
import com.actionbarsherlock.app.SherlockActivity; 
//import com.actionbarsherlock.app.SherlockFragmentActivity; 
import com.wts.ui.MainActivity.OnContextItemSelectedListener; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 


public class TabHostActivity extends SherlockActivity 
implements OnContextItemSelectedListener 
{ 

    private ActionBar actionBar; 

    protected final static int REQUEST_CODE = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     SettingsManager.setPreferedTheme(this);//setTheme  

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tab_host);  

     actionBar = getSupportActionBar(); 
     actionBar.setDisplayOptions(ActionBar.NAVIGATION_MODE_TABS); 
     actionBar.setDisplayShowTitleEnabled(false); 

     Tab tabA = actionBar.newTab().setText("A Tab"); 

     Fragment fragmentC = new MainActivity(); 
     tabA.setTabListener(new MyTabsListener(fragmentC)); 
     actionBar.addTab(tabA);    

    @Override 
    public void StartSherlockActivity(Intent intent) {  
      startActivityForResult(intent, REQUEST_CODE); 
      //saveInstance of tab position 
      } 

    public static class MyTabListener<T extends Fragment> implements 
    ActionBar.TabListener { 
    private Fragment mFragment; 
    private final SherlockActivity mActivity; 
    private final String mTag; 
    private final Class<T> mClass; 


/** 
    * Constructor used each time a new tab is created. 
    * 
    * @param activity 
    *   The host Activity, used to instantiate the fragment 
    * @param tag 
    *   The identifier tag for the fragment 
    * @param clz 
    *   The fragment's Class, used to instantiate the fragment 
    */ 

    public MyTabListener(SherlockActivity activity, String tag, Class<T> clz) { 
    mActivity = activity; 
    mTag = tag; 
    mClass = clz; 
    } 

    /* The following are each of the ActionBar.TabListener callbacks */ 

    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
    // Check if the fragment is already initialized 
    if (mFragment == null) { 
     // If not, instantiate and add it to the activity 
     mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
     ft.add(R.layout.activity_tab_host, mFragment, mTag); 
    } else { 
     // If it exists, simply attach it in order to show it 
//  ft.setCustomAnimations(android.R.animator.fade_in, 
//   R.animator.animationtest); 
     ft.attach(mFragment); 
    } 
    } 

    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
    if (mFragment != null) { 
//  ft.setCustomAnimations(android.R.animator.fade_in, 
//   R.animator.test); 
     ft.detach(mFragment); 
    } 
    } 

    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
    } 
} 

    protected class MyTabsListener implements ActionBar.TabListener { 

     private Fragment fragment; 

     public MyTabsListener(Fragment fragment) { 
      this.fragment = fragment; 
     } 

     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     } 

     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      ft.add(R.id.fragment_container, fragment, null); 
     } 

     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
      // some people needed this line as well to make it work: 
      ft.remove(fragment); 
     } 
    } 

} 

其佈局XML:

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

和碎片類:

public class MainActivity extends Fragment { 

    public interface OnContextItemSelectedListener{ 
     public void StartSherlockActivity(Intent intent);  
    } 
    private OnContextItemSelectedListener onContextMenuListener; 


    //protected final static int REQUEST_CODE = 1; 

    public static WordsDBAdapter dbAdapter; 
    private CustomAdapter cDataAdapter; 
    private Button button; 
    private EditText editWord; 
    private EditText editTranslate; 
    private ListView listView; 
    private String selectedWord; 
    private Cursor cursor; 

    // context menu 
    private final static int IDM_EDIT = 101; 
    private final static int IDM_DELETE = 102; 
    private final static int IDM_INFO = 103; 

    // options menu 
    private static final int IDM_ABOUT = 201; 
    private static final int IDM_EXIT = 202; 
    private static final int IDM_SETTINGS = 203; 
    private static final int IDM_QUESTION = 204; 

    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { 

     // Called when the action mode is created; startActionMode() was called 
     @Override 
     public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
      // Inflate a menu resource providing context menu items 
      MenuInflater inflater = mode.getMenuInflater(); 
      inflater.inflate(R.menu.context_menu_list, menu); 
      return false; 
     } 

     // Called each time the action mode is shown. Always called after 
     // onCreateActionMode, but 
     // may be called multiple times if the mode is invalidated. 
     @Override 
     public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
      return false; 
     } 

     @Override 
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 

      switch(item.getItemId()) 
      { 

      case R.id.Edit_cm: 
       Intent intent = new Intent(getActivity(), EditActivity.class); 
       intent.putExtra(getResources().getString(R.string.fstRow), 
       cursor.getString(WordsDBAdapter.WORD_COL)); 
       intent.putExtra(getResources().getString(R.string.scndRow), 
       cursor.getString(WordsDBAdapter.TRANS_COL)); 
       intent.putExtra(getResources().getString(R.string.thrdRow), 
       cursor.getString(WordsDBAdapter.DESC_COL)); 
       onContextMenuListener.StartSherlockActivity(intent); 
       break; 
      case R.id.Info_cm: 
       break; 
      case R.id.Delete_cm: 
       break; 

      default:return false; 
      } 
      return false; 
     } 

     @Override 
     public void onDestroyActionMode(ActionMode mode) { 


     } 

    }; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     final View view = inflater.inflate(R.layout.activity_about, container, 
       false); 

     dbAdapter = new WordsDBAdapter(getActivity()); 
     dbAdapter.open(); 

     button = (Button) view.findViewById(R.id.buttonAddWord); 

     listView = (ListView) view.findViewById(R.id.listWords); 
     displayListView(); 
     registerForContextMenu(listView); 

     // ================ListView onLongClick======================== 
     listView.setOnItemLongClickListener(new OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       cursor = (Cursor) listView.getItemAtPosition(arg2); 
       selectedWord = cursor.getString(WordsDBAdapter.ID_COL); 
       return false; 
      } 
     }); 

     // ================Button onClick======================== 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       editWord = (EditText) view.findViewById(R.id.editWord); 
       editTranslate = (EditText) view 
         .findViewById(R.id.editTranslate); 

       String word = editWord.getText().toString(); 
       String translate = editTranslate.getText().toString(); 

       if (word.length() > 0 && translate.length() >= 0) { 
        Cursor cursor = dbAdapter.fetchWordsByName(word);// chek is 
                     // word 
                     // repeat 

        if (cursor.moveToFirst()) { 
         Toast.makeText(getActivity().getApplicationContext(), 
           getResources().getString(R.string.word_exist), 
           Toast.LENGTH_SHORT).show(); 
        } else if (!CheckWordInput(word) 
          || !CheckTranslateInput(translate)) { 
         Toast.makeText(
           getActivity().getApplicationContext(), 
           getResources().getString(
             R.string.incorrect_input), 
           Toast.LENGTH_SHORT).show(); 
        } else { 
         dbAdapter.insertWord(word, translate, " ", 
           String.valueOf(false), 0, 0, new Date()); 
         displayListView(); 

         editWord.setText(""); 
         editTranslate.setText(""); 
        } 
       } 
      } 
     }); 

     return view; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // SettingsManager.setPreferedTheme(this);//setTheme 

     super.onCreate(savedInstanceState); 
     // setContentView(R.layout.activity_main); 

    } 



    @Override 
     public void onAttach(Activity activity) { 
      super.onAttach(activity); 
      try { 
       onContextMenuListener = (OnContextItemSelectedListener) activity; 
      } catch (ClassCastException e) { 
       throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); 
      } 
     } 
    private void displayListView() { 
     // Cursor cursor = dbAdapter.fetchAllTranslated(); 
     Cursor cursor = dbAdapter.fetchAllTranslated(); 

     String[] columns = new String[] { WordsDBAdapter.KEY_WORD, 
       WordsDBAdapter.KEY_TRANSLATION, WordsDBAdapter.KEY_SUCCEEDED, }; 

     int[] to = new int[] { R.id.textViewTranslate, R.id.textViewWord, 
       R.id.textViewSuccessPoints }; 

     cDataAdapter = new CustomAdapter((Activity) listView.getContext(), 
       R.layout.word_info, cursor, columns, to); 

     listView.setAdapter(cDataAdapter); 
    } 


    @Override 
    public void onResume() { 
     super.onResume(); 
     // SettingsManager.setPreferedLanguage(this);// set language 
     displayListView(); 
    } 

    public static boolean CheckTranslateInput(String str) { 
     Pattern inputPattern = Pattern.compile("[\\p{L} -]{0,25}"); 
     Matcher inputMatcher = inputPattern.matcher(str); 
     return inputMatcher.matches(); 
    } 

    public static boolean CheckWordInput(String str) { 
     Pattern inputPattern = Pattern.compile("[\\p{L} -]{1,25}"); 
     Matcher inputMatcher = inputPattern.matcher(str); 
     return inputMatcher.matches(); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     dbAdapter.close(); 
    }   

} 

其佈局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/buttonAddWord" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignBottom="@+id/editTranslate" 
     android:layout_alignTop="@+id/editTranslate" 
     android:layout_toRightOf="@id/editTranslate" 
     android:drawableTop="@drawable/plus" /> 

    <TextView 
     android:id="@+id/textViewEditTranslateDescription" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textViewEditWordDescription" 
     android:layout_alignLeft="@+id/editTranslate" 
     android:text="@string/scndRow" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/textViewEditWordDescription" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/editTranslate" 
     android:layout_alignParentLeft="true" 
     android:text="@string/fstRow" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <EditText 
     android:id="@+id/editTranslate" 
     android:layout_width="140dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_toRightOf="@+id/editWord" 
     android:ems="10" 
     android:inputType="textCapSentences" /> 

    <EditText 
     android:id="@+id/editWord" 
     android:layout_width="140dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:ems="10" 
     android:inputType="textCapSentences" >   
    </EditText> 

    <ListView 
     android:id="@+id/listWords" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textViewEditTranslateDescription" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 

    </ListView> 

</RelativeLayout> 

對不起巨大的代碼,但它沒有問題,onCreateView()或其他一些覆蓋過程中爲什麼我做錯了,爲什麼我的MainActivity內容沒有出現,我不能看到Tabs,屏幕上只顯示了actionBar和app圖標。我的代碼有什麼問題?

回答

0

您使用什麼設備?如果您使用模擬器,API的級別是多少?

如果API低於3 Android不支持操作欄。只有當API等於3或更高時,NoActionBar主題纔會適用。

根據我的經驗,當在Nexus API 2.3中移植時,即使我想要顯示操作欄,屏幕上方只有一個細條。

希望這會有所幫助。

相關問題