我正在使用安装了viewpager
的Android Fragments。基本上你可以刷过5个标签。
5中的4个包含由MySQL表填充的ListFragments
;这是在AsyncTask
内完成的。当我快速滑动时,我有时会收到此错误:
09-01 07:54:01.243: E/AndroidRuntime(19706): FATAL EXCEPTION: main
09-01 07:54:01.243: E/AndroidRuntime(19706): java.lang.IllegalStateException: Content view not yet created
09-01 07:54:01.243: E/AndroidRuntime(19706): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
09-01 07:54:01.243: E/AndroidRuntime(19706): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
09-01 07:54:01.243: E/AndroidRuntime(19706): at com.---.---.MasterCat$TopFrag$TopTask.onPostExecute(MasterCat.java:570)
09-01 07:54:01.243: E/AndroidRuntime(19706): at com.---.---.MasterCat$TopFrag$TopTask.onPostExecute(MasterCat.java:1)
09-01 07:54:01.243: E/AndroidRuntime(19706): at android.os.AsyncTask.finish(AsyncTask.java:631)
09-01 07:54:01.243: E/AndroidRuntime(19706): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-01 07:54:01.243: E/AndroidRuntime(19706): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-01 07:54:01.243: E/AndroidRuntime(19706): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 07:54:01.243: E/AndroidRuntime(19706): at android.os.Looper.loop(Looper.java:137)
09-01 07:54:01.243: E/AndroidRuntime(19706): at android.app.ActivityThread.main(ActivityThread.java:4928)
09-01 07:54:01.243: E/AndroidRuntime(19706): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 07:54:01.243: E/AndroidRuntime(19706): at java.lang.reflect.Method.invoke(Method.java:511)
09-01 07:54:01.243: E/AndroidRuntime(19706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-01 07:54:01.243: E/AndroidRuntime(19706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-01 07:54:01.243: E/AndroidRuntime(19706): at dalvik.system.NativeStart.main(Native Method)
我故意没有使用ProgressDialog
,因为它会使刷卡口吃不少。
所以我有两个选择,我需要帮助:
如何防止此错误并在刷卡时保持列表刷新。
或者在加载5个片段后,关闭“刷卡时刷新”。换句话说,除非您退出活动并返回,否则列表将保持不变。
我更喜欢选项#1。请告诉我您需要查看的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0: {
f = new MasterFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
case 1: {
f = new FeaturedFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
case 4: {
f = new TopFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
case 3: {
f = new NewFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
case 2: {
f = new TrendFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
default:
throw new IllegalArgumentException("not this many fragments: "
+ position);
}
return f;
}
此外,LogCat中引用的第570行属于AsyncTask内部的getListView(),它位于上面未显示的ListFragment内部。
修改:
添加5个片段中的一个 - 在LogCat中引用的片段 - “TopFrag”
class TopTask extends AsyncTask<String, String, Void> {
InputStream is = null;
String result = "";
@Override
protected Void doInBackground(String... params) {
String url_select = "http://www.---.com/---/---.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("Top", "Top"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
protected void onPostExecute(Void v) {
String item, cat;
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
item = json_data.getString("item");
cat = json_data.getString("category");
items.add(item);
cats.add(cat);
}
} catch (JSONException e1) {
Toast.makeText(getActivity(), "No Top Items Found",
Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
ListView listView;
listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long id) {
// edit
}
});
MasterCatObject[] mco = new MasterCatObject[items.size()];
int index = 0;
for (@SuppressWarnings("unused")
String i : items) {
mco[index] = new MasterCatObject(items.get(index),
cats.get(index));
index++;
}
adapter = new AllTimeAdapter(getActivity(), mco);
setListAdapter(adapter);
}
}
}
答案 0 :(得分:23)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.---.---.MasterCat$TopFrag$TopTask.onPostExecute(MasterCat.java:570)
您的TopTask.onPostExecute()
在片段尚未显示(onCreateView
之前)/不再显示(onDestroyView
之后)时,不得触摸UI。你无法获得ListView
。
您可以在此处更新ListView
使用的数据结构,以便下次绘制列表时包含新数据。
答案 1 :(得分:0)
为避免崩溃,可以检查ListFragment是否已添加())
在异常时刻分离了ListFragment。