问题描述
我正在编写的应用程序有 3 个片段。当我按下搜索按钮时,主片段活动有一个带搜索按钮的搜索框,调用以下函数:
Fragments.values()[tabControl.getCurrentItem()].getFragment().search(tv.getText().toString(), MainActivity.this.getApplicationContext());
一切正常,直到我按下 HOME 按钮等待 30 分钟并重新启动应用程序,在我按下FragmentYellowPages
Fragment 应用程序中的搜索按钮后,应用程序崩溃了
lvYellowPages.setVisibility(View.VISIBLE);
因为lvYellowPages
在这种情况下为空,但在日志中我可以看到该onCreateView
函数被调用。你能帮帮我吗,我可以和你分享我的代码。
FragmentPagerAdapter
public class FragmentAdapter extends FragmentPagerAdapter {
Fragments[] mFragments;
public FragmentAdapter(FragmentManager fm) {
super(fm);
mFragments = Fragments.values();
}
public enum Fragments {
Favorites(App.getStringByResId(R.string.favorites), new FragmentFavorites()),
Categories(App.getStringByResId(R.string.categories), new FragmentCategories()),
YellowPages(App.getStringByResId(R.string.yellow_pages), new FragmentYellowPages());
Fragments(String title, BaseListFragment fragment) {
this.mTitle = title;
this.mFragment = fragment;
}
String mTitle;
BaseListFragment mFragment;
public String getTitle() {
return mTitle;
}
public BaseListFragment getFragment() {
return mFragment;
}
};
@Override
public CharSequence getPageTitle(int position) {
return mFragments[position].getTitle();
}
@Override
public Fragment getItem(int position) {
return mFragments[position].getFragment();
}
@Override
public int getCount() {
return mFragments.length;
}
};
列出片段
public class FragmentYellowPages extends BaseListFragment {
final static String TAG = FragmentYellowPages.class.getSimpleName();
ListView lvYellowPages;
TextView tvInfo;
SimpleCursorAdapter scAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View yellowPagesView = inflater.inflate(R.layout.fragment_yellow_pages, container, false);
Log.i(TAG, "onCreateView( )");
tvInfo = (TextView) yellowPagesView.findViewById(R.id.tvYellowPagesInfo);
lvYellowPages = (ListView) yellowPagesView.findViewById(android.R.id.list);
return yellowPagesView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate( )");
setRetainInstance(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated( )");
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume( )");
}
void updateAdapterOnSearch(final String tts, final String lang) {
Log.i(TAG, String.format("updateAdapterOnSearch(%s, %s)", tts, lang));
/* Show "Yellow Pages" list view and hide Info text view. */
lvYellowPages.setVisibility(View.VISIBLE);
tvInfo.setVisibility(View.GONE);
/* Some code .... */
}
@Override
public void search(final String tts, final Context context) {
Log.i(TAG, String.format("search(%s)", tts));
/* If user search for the text. */
if (tts != null && tts.length() != 0) {
updateAdapterOnSearch(tts, lang);
}
}
};
片段布局
<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:background="#ffffc8"
android:tag="FRAGMENT_YELLOW_PAGES_TAG" >
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="#ffffc8"/>
<TextView android:id="@+id/tvYellowPagesInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="5dp"
android:text="@string/no_companies_were_found"
android:visibility="gone"/>
</RelativeLayout>