gpt4 book ai didi

在 ListFragment 中填充自定义 View 时出现 Android U_MISSING_RESOURCE_ERROR

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:06 24 4
gpt4 key购买 nike

我试图将这两个源代码(http://stackoverflow.com/questions/9697716/listview-displays-each-item-twice-after-filtering 和 http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/)结合起来,以便制作两个选项卡,其中其中之一有一个带有搜索框的 ListView 。

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 看起来像:

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

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

有谁知道这意味着什么以及我该如何解决它?

最好的问候,罗马。

最佳答案

我通过将 allservices.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: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 就可以了。我认为如果没有 FrameLayout,Listview 会使 Edittext 消失,因此会抛出 U_MISSING_RESOURCE_ERROR。

关于在 ListFragment 中填充自定义 View 时出现 Android U_MISSING_RESOURCE_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10270041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com