2012-04-22 8 views
4

これらの2つのソースコードを組み合わせようとしました(http://stackoverflow.com/questions/9697716/listview-displays-each-item-twice-after-filteringおよびhttp://android.codeandmagic.org/2011/07/android-tabs-with-fragments/)それらのうちの1つが検索ボックスでリストビューを持つ2つのタブを作成します。ListFragmentでカスタムビューを作成する場合のAndroid U_MISSING_RESOURCE_ERROR

public class TabsFragment extends Fragment implements OnTabChangeListener { 

private static final String TAG = "FragmentTabs"; 
public static final String TAB_ALLSERVICES = "allservices"; 
public static final String TAB_MOSTUSEDSERVICES = "mostusedservices"; 

private View mRoot; 
private TabHost mTabHost; 
private int mCurrentTab; 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    mRoot = inflater.inflate(R.layout.tabs_fragment, null); 
    mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost); 
    setupTabs(); 
    return mRoot; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    setRetainInstance(true); 

    mTabHost.setOnTabChangedListener(this); 
    mTabHost.setCurrentTab(mCurrentTab); 
    // manually start loading stuff in the first tab 
    updateTab(TAB_ALLSERVICES, R.id.tab_1); 
} 

private void setupTabs() { 
    mTabHost.setup(); // important! 
    mTabHost.addTab(newTab(TAB_ALLSERVICES, R.string.tab_allservices, 
      R.id.tab_1)); 
    mTabHost.addTab(newTab(TAB_MOSTUSEDSERVICES, 
      R.string.tab_mostusedservices, R.id.tab_2)); 
} 

private TabSpec newTab(String tag, int labelId, int tabContentId) { 
    Log.d(TAG, "buildTab(): tag=" + tag); 

    View indicator = LayoutInflater.from(getActivity()).inflate(
      R.layout.tab, 
      (ViewGroup) mRoot.findViewById(android.R.id.tabs), false); 
    ((TextView) indicator.findViewById(R.id.text)).setText(labelId); 

    TabSpec tabSpec = mTabHost.newTabSpec(tag); 
    tabSpec.setIndicator(indicator); 
    tabSpec.setContent(tabContentId); 
    return tabSpec; 
} 

@Override 
public void onTabChanged(String tabId) { 
    Log.d(TAG, "onTabChanged(): tabId=" + tabId); 
    if (TAB_ALLSERVICES.equals(tabId)) { 
     updateTab(tabId, R.id.tab_1); 
     mCurrentTab = 0; 
     return; 
    } 
    if (TAB_MOSTUSEDSERVICES.equals(tabId)) { 
     updateTab(tabId, R.id.tab_2); 
     mCurrentTab = 1; 
     return; 
    } 

} 

private void updateTab(String tabId, int placeholder) { 
    FragmentManager fm = getFragmentManager(); 
    if (fm.findFragmentByTag(tabId) == null) { 
     if (tabId.equals(TAB_MOSTUSEDSERVICES)) { 
      fm.beginTransaction() 
        .replace(placeholder, new MostUsedServicesFragment(), tabId) 
        .commit(); 
     } else if (tabId.equals(TAB_ALLSERVICES)) { 
      fm.beginTransaction() 
      .replace(placeholder, new Fragmenttest(), tabId) 
      .commit(); 
     } 
    } 
} 
} 

tabs_fragment.xml looks like following: 
<?xml version="1.0" encoding="utf-8"?> 
<TabHost 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#EFEFEF"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <FrameLayout 
       android:id="@+id/tab_1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 

      <FrameLayout 
       android:id="@+id/tab_2" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 

     </FrameLayout> 
</LinearLayout> 
</TabHost> 

public class Fragmenttest extends ListFragment { 
ListView newReqList; 
LayoutInflater inflater; 
String[] from = new String[] { "mainrow", "subrow" }; 
EditText searchBar = null; 
ArrayAdapter<String> sAdapter = null; 
private static final String[] NUMBERS = { "1", "2", "3", "4", "5", "VI", 
     "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV" }; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    newReqList = this.getListView(); 

    searchBar = (EditText) this.getActivity().findViewById(R.id.search_box); 
    List<String> l = new ArrayList<String>(); 

    for (String s : NUMBERS) { 
     l.add(s); 
    } 
    sAdapter = new ArrayAdapter<String>(this.getActivity(), 
      R.layout.list_item, l); 
    newReqList.setAdapter(sAdapter); 
    searchBar.addTextChangedListener(filterTextWatcher); 
} 

private TextWatcher filterTextWatcher = new TextWatcher() { 

    public void afterTextChanged(Editable s) { 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 

    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     sAdapter.getFilter().filter(s); 
     sAdapter.notifyDataSetChanged(); 
    } 

}; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.allservices, container, false); 
    return v; 
} 
} 

allservices.xmlは、次のようになります。

04-19 04:59:04.276: E/AndroidRuntime(4106): FATAL EXCEPTION: main 
04-19 04:59:04.276: E/AndroidRuntime(4106): java.lang.RuntimeException: U_MISSING_RESOURCE_ERROR 

誰もがそれが何を意味するのか知っていますし、どのようにI:できるだけ早く私は、アプリケーションを実行するよう

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<!-- Pretty hint text, and maxLines --> 

<EditText 
    android:id="@+id/search_box" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/allserviceshint" 
    android:inputType="text" 
    android:maxLines="1" /> 

<!-- Set height to 0, and let the weight param expand it --> 
<!-- 
    Note the use of the default ID! This lets us use a 
    ListActivity still! 
--> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" /> 
</LinearLayout> 

、私はLogCatを以下の取得しますそれを修正することはできますか?

最高のお付き合い

+0

完全なスタックトレースを追加してください –

+0

ご回答ありがとうございますが、私はすでに問題を解決しました。完全なスタックトレースに興味がある場合は、古いxmlファイルを使用してアプリケーションを再起動し、ここに投稿することができます。 – Roman

答えて

1

私は次のようにallservices.xmlを変更することで問題を解決:

トリックをしたでframeLayoutの追加
<?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:background="@android:drawable/gallery_thumb" 
android:orientation="vertical" > 

<EditText 
    android:id="@+id/search" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="Hallo" 
    android:inputType="text" 
    android:maxLines="1" /> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:drawSelectorOnTop="false" /> 

    <TextView 
     android:id="@android:id/empty" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="No items." 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
</FrameLayout> 
</LinearLayout> 

。私はFrameLayoutなしでListviewはEdittextを消してしまったと思うので、U_MISSING_RESOURCE_ERRORがスローされました。

関連する問題