我正在尝试在选项卡片段中的listview上实现延迟加载,以加载数组abc的元素。我正在尝试加载10个元素,然后当用户向下滚动时,将加载下10个元素。但我的应用程序无法正常工作,因为我无法跳转到UI线程以便更新它(runonUithread(returnRes);不工作)。告诉你[请看下面的代码并建议实现延迟加载的正确方法。谢谢。
片段类:
package com.example.abe;
import java.util.ArrayList;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Tab1Fragment extends ListFragment {
ListView lv;
Activity act = this.getActivity();
Context ct = this.getActivity();
XMLGettersSetters data;
boolean loadingMore = false;
String abc[] = new String[50];
static int count = 0;
int itemsPerPage = 10;
ArrayList<String> myListItems;
ArrayList<String> myListItems2;
ArrayAdapter<String> adapter;
static int size = 0;
@SuppressWarnings("unused")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
myListItems2 = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
android.R.layout.simple_list_item_1, myListItems2);
View root = inflater.inflate(R.layout.tab_frag1_layout, container,
false);
lv = (ListView) root.findViewById(android.R.id.list);
lv.setAdapter(adapter);
return root;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
myListItems = new ArrayList<String>();
View footerView = ((LayoutInflater) this.getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.listfooter, null, false);
this.getListView().addFooterView(footerView);
this.setListAdapter(adapter);
for (int i = 0; i < 50; i++) {
abc[i] = "ab" + i;
}
this.getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
System.out.println(abc.length);
System.out.println(count);
int lastInScreen = firstVisibleItem + visibleItemCount;
System.out.println(lastInScreen);
if ((lastInScreen == totalItemCount) && !(loadingMore)) {
try {
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
try {
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private Runnable loadMoreListItems = new Runnable() {
@Override
public void run() {
loadingMore = true;
myListItems = new ArrayList<String>();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
size = abc.length;
for (int i = 0; i < itemsPerPage; i++) {
if (count < size) {
myListItems.add(abc[count].toString());
count = count + 1;
} else {
break;
}
}
Log.i("a", "b");
try {
act.runOnUiThread(returnRes);
} catch (Exception e) {
e.printStackTrace();
}
Log.i("a", "b");
}
};
private Runnable returnRes = new Runnable() {
@Override
public void run() {
if (myListItems != null && myListItems.size() > 0) {
for (int i = 0; i < myListItems.size(); i++)
adapter.add(myListItems.get(i));
}
lv.setAdapter(adapter);
Log.i("a", "b");
adapter.notifyDataSetChanged();
Log.i("a", "b");
loadingMore = false;
}
};
}
logcat的:
01-18 18:59:21.606: I/System.out(971): 50
01-18 18:59:21.606: I/System.out(971): 0
01-18 18:59:21.606: I/System.out(971): 0
01-18 18:59:21.926: I/System.out(971): 50
01-18 18:59:21.926: I/System.out(971): 0
01-18 18:59:21.926: I/System.out(971): 1
01-18 18:59:22.257: I/System.out(971): 50
01-18 18:59:22.257: I/System.out(971): 0
01-18 18:59:22.257: I/System.out(971): 1
01-18 18:59:22.676: I/a(971): b
01-18 18:59:22.676: W/System.err(971): java.lang.NullPointerException
01-18 18:59:22.715: W/System.err(971): at com.example.abe.Tab1Fragment$1.run(Tab1Fragment.java:113)
01-18 18:59:22.936: W/System.err(971): at java.lang.Thread.run(Thread.java:1019)
01-18 18:59:22.936: I/a(971): b
FragmentActivity类:
package com.example.abe;
import java.util.HashMap;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
public class TabsFragmentActivity extends FragmentActivity implements
TabHost.OnTabChangeListener {
private TabHost mTabHost;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsFragmentActivity.TabInfo>();
private TabInfo mLastTab = null;
private class TabInfo {
private String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
class TabFactory implements TabContentFactory {
private final Context mContext;
public TabFactory(Context context) {
mContext = context;
}
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_layout);
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); // set
// the
// tab
// as
// per
// the
// saved
// state
}
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
// selected
super.onSaveInstanceState(outState);
}
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab1").setIndicator("ST"), (tabInfo = new TabInfo(
"Tab1", Tab1Fragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab2").setIndicator("CSI"),
(tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
this.onTabChanged("Tab1");
mTabHost.setOnTabChangedListener(this);
}
private static void addTab(TabsFragmentActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec, TabInfo tabInfo) {
tabSpec.setContent(activity.new TabFactory(activity));
String tag = tabSpec.getTag();
tabInfo.fragment = activity.getSupportFragmentManager()
.findFragmentByTag(tag);
if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
FragmentTransaction ft = activity.getSupportFragmentManager()
.beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
tabHost.addTab(tabSpec);
}
public void onTabChanged(String tag) {
TabInfo newTab = this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager()
.beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this,
newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
}
答案 0 :(得分:1)
使用Endless Scrolling ListView。 Here is the link。我已经实现了它。如果你有任何疑问,你可以问。
这是我的TabActivity
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
Context context = MainActivity.this;
TabHost tabHost;
TabSpec spec;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
// Android tab
tabHost.addTab(tabHost
.newTabSpec("Home")
.setIndicator("Home",
getResources().getDrawable(R.drawable.home))
.setContent(
new Intent(this, MyActivityGroup.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost
.newTabSpec("Now Reading")
.setIndicator("Now Reading",
getResources().getDrawable(R.drawable.now_reading))
.setContent(
new Intent(this, NowReadingActivityGroup.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost
.newTabSpec("Favorites")
.setIndicator("Favorites",
getResources().getDrawable(R.drawable.favorites))
.setContent(
new Intent(this, FavoriteActivityGroup.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost
.newTabSpec("Profile")
.setIndicator("Profile",
getResources().getDrawable(R.drawable.profile))
.setContent(
new Intent(this, ProfileActivityGroup.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.setCurrentTabByTag("Home");
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这里是ActivityGroup类
import java.util.Stack;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.Window;
public class MyActivityGroup extends ActivityGroup {
private Stack<String> stack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (stack == null) {
stack = new Stack<String>();
}
push("1stStackActivity", new Intent(this, Home.class));
}
@Override
public void finishFromChild(Activity child) {
pop();
}
@Override
public void onBackPressed() {
pop();
}
public void push(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(id,
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
stack.push(id);
setContentView(window.getDecorView());
}
}
public void pop() {
if (stack.size() == 1) {
Home.BookTitle = null;
Home.BookCoverPhotos = null;
Home.BookAuther = null;
Home.BookPublishDate = null;
Home.language = "all";
Home.bitmapArray = null;
Home.BookIDs = null;
Home.BookRating = null;
Home.BookDescription = null;
Home.BookCode = null;
Home.userFName = null;
Home.gridViewState = null;
Home.initialIndex = 0;
Home.CustomerID = -1;
Home.checkBookCount = true;
Home.str = null;
Home.FlagSharedPreferences = false;
BookSearchResultActivity.BookTitle = null;
BookSearchResultActivity.BookCoverPhotos = null;
BookSearchResultActivity.BookAuther = null;
BookSearchResultActivity.BookPublishDate = null;
BookSearchResultActivity.ImageByte = null;
BookSearchResultActivity.bitmapArray = null;
BookSearchResultActivity.BookIDs = null;
BookSearchResultActivity.BookRating = null;
BookSearchResultActivity.BookDescription = null;
BookSearchResultActivity.startIndex = 0;
BookSearchResultActivity.endIndex = 14;
BookSearchResultActivity.listViewState = null;
finish();
}
LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity(stack.pop(), true);
if (stack.size() > 0) {
Intent lastIntent = manager.getActivity(stack.peek()).getIntent()
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window newWindow = manager.startActivity(stack.peek(), lastIntent);
setContentView(newWindow.getDecorView());
}
}
}
这是我的Home Activty Class,它使用EndlessLisnter进行gridview,但你也可以将它用于listview。
public class Home extends Activity { /** 在第一次创建活动时调用。 * / static final String URL =“http://www.shiaislamiclibrary.com/requesthandler.ashx”;
static final String KEY_ITEM = "Book"; // parent node
static final String KEY_BOOKAUTHOR = "book_author";
static final String KEY_BOOKRATING = "BookRating";
static final String KEY_BOOKID = "BookID";
static final String KEY_BOOKDESC = "BookDescription";
static final String KEY_BOOKDATEPUBLISHED = "DatePublished";
static final String KEY_BOOKTITLE = "BookTitle";
static final String KEY_BOOKCODE = "BookCode";
static final String KEY_BOOKIMAGE = "BookImage";
static ArrayList<String> BookTitle = null;
static ArrayList<Integer> BookRating = null;
static ArrayList<String> BookDescription = null;
static ArrayList<String> BookCoverPhotos = null;
static ArrayList<String> BookAuther = null;
static ArrayList<String> BookIDs = null;
static ArrayList<String> BookCode = null;
static ArrayList<String> BookPageCount = null;
static ArrayList<String> BookPublishDate = null;
static ArrayList<Bitmap> bitmapArray = null;
static String str = null;
Context ctx = this;
Activity act = this;
Context context = Home.this;
URL bookImageURL = null;
Bitmap bitMapImage = null;
LayoutInflater inflater = null;
GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.home_activity);
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(
R.layout.home_activity, null);
this.setContentView(viewToLoad);
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
gridView = (GridView) findViewById(R.id.gridview);
gridView.setOnScrollListener(new EndlessScrollListener());
if (gridViewState != null) {
gridView.onRestoreInstanceState(gridViewState);
}
public void checkConnection() {
// Check Flight mode ON/OFF
if (Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 0) {
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
// Avoid to reload the page again and again
if (str == null) {
str = "TempString";
BookTitle = new ArrayList<String>();
BookRating = new ArrayList<Integer>();
BookDescription = new ArrayList<String>();
BookIDs = new ArrayList<String>();
BookCode = new ArrayList<String>();
BookCoverPhotos = new ArrayList<String>();
BookAuther = new ArrayList<String>();
BookPublishDate = new ArrayList<String>();
BookPageCount = new ArrayList<String>();
bitmapArray = new ArrayList<Bitmap>();
new UIThread().execute(URL, initialIndex + "");
} else {
ImageAdapter adapter = new ImageAdapter(context, act);
gridView.setAdapter(adapter);
if (gridViewState != null) {
gridView.onRestoreInstanceState(gridViewState);
}
btnLanguage.setText(language);
}
} else {
connectionFailureDialog("CONNECTION FAILURE...!",
"No internet connection found");
}
} else {
new DialogClass(getParent()).airplaneDialog("AIRPLANE MODE...!",
"Please make sure Airplane mode is Turned Off", "Exit");
}
}
@Override
protected void onStart() {
checkConnection();
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
Intent bookOverViewIntent = new Intent();
bookOverViewIntent.setClass(getParent(), BookOverView.class);
bookOverViewIntent.putExtra("BITMAP", bitmapArray.get(pos));
bookOverViewIntent.putExtra("BOOK_TITLE", BookTitle.get(pos));
bookOverViewIntent.putExtra("BOOK_AUTHOR", BookAuther.get(pos));
bookOverViewIntent.putExtra("BOOK_PUBLISH_DATE",
BookPublishDate.get(pos));
bookOverViewIntent.putExtra("BOOK_ID", BookIDs.get(pos));
bookOverViewIntent.putExtra("BOOK_RATING", BookRating.get(pos));
bookOverViewIntent.putExtra("BOOK_DESC",
BookDescription.get(pos));
bookOverViewIntent.putExtra("PAGE_NO", "1");
bookOverViewIntent.putExtra("BOOK_CODE", BookCode.get(pos));
bookOverViewIntent.putExtra("BOOK_PAGE_COUNT",
BookPageCount.get(pos));
bookOverViewIntent.putExtra("ACTIVITY", "Home");
MyActivityGroup activityStack = (MyActivityGroup) getParent();
activityStack.push("BookOverView", bookOverViewIntent);
gridViewState = gridView.onSaveInstanceState();
}
});
private class listAdapter extends BaseAdapter {
int size;
@Override
public int getCount() {
if (btnFlag.equals("btnLanguage")) {
size = getResources().getStringArray(
R.array.spnr_language_array).length;
} else if (btnFlag.equals("btnBrowseBy")) {
size = getResources().getStringArray(R.array.spnr_browse_array).length;
}
return size;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
LayoutInflater inflater;
if (btnFlag.equals("btnLanguage")) {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.itemslist, parent, false);
TextView listText = (TextView) row.findViewById(R.id.textView2);
listText.setText(getResources().getStringArray(
R.array.spnr_language_array)[position]);
} else if (btnFlag.equals("btnBrowseBy")) {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.itemslist, parent, false);
TextView listText = (TextView) row.findViewById(R.id.textView2);
listText.setText(getResources().getStringArray(
R.array.spnr_browse_array)[position]);
}
return row;
}
}
private class EndlessScrollListener implements OnScrollListener {
private int visibleThreshold = 0;
private int currentPage = 0;
private int previousTotal = 0;
private boolean loading = true;
public EndlessScrollListener() {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
currentPage++;
}
}
if (!loading
&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
if (checkBookCount) {
new UIThread().execute(URL, initialIndex + "");
Log.i("Reached If", "End");
}
loading = true;
gridViewState = gridView.onSaveInstanceState();
}
}
private class UIThread extends AsyncTask<String, Integer, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(getParent(),
"Acumlating Books from server...",
"This may Take a few seconds.\nPlease Wait...");
}
@Override
protected String doInBackground(String... params) {
String URL = params[0];
int lIndex = Integer.valueOf(params[1]);
XMLParser parser = new XMLParser();
String XMLString = null;
XMLString = parser.getXmlFromUrl_FeaturedBooks(URL, lIndex,
language);
// Log.i("language = ", language);
// Log.i("XMLString = ", XMLString);
if (XMLString != null) {
initialIndex = lIndex + 14;
Document doc = parser.getDomElement(XMLString);
NodeList nlBooksLimit = doc
.getElementsByTagName(KEY_ITEM_BOOKs_LIMIT);
Element eLimit = (Element) nlBooksLimit.item(0);
totalBookCount = 0;
try {
totalBookCount = Integer.valueOf(parser.getValue(eLimit,
KEY_ITEM_TOTAL_BOOKS));
checkBookCount = true;
} catch (NumberFormatException e) {
checkBookCount = false;
totalBookCount = totalBookCount + 0;
}
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
Bitmap imageNotFound = BitmapFactory.decodeResource(
getResources(), R.drawable.defaultcoverphoto);
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
try {
BookRating.add(Integer.valueOf(parser.getValue(e,
KEY_BOOKRATING)));
} catch (Exception e2) {
BookRating.add(0);
}
BookDescription.add(parser.getValue(e, KEY_BOOKDESC));
BookTitle.add(parser.getValue(e, KEY_BOOKTITLE));
BookCoverPhotos
.add("http://shiaislamicbooks.com/books_Snaps/"
+ parser.getValue(e, KEY_BOOKCODE)
+ "/1_thumb.jpg");
int tempCount = BookCoverPhotos.size() - 1;
BookAuther.add(parser.getValue(e, KEY_BOOKAUTHOR));
BookPublishDate.add(parser.getValue(e,
KEY_BOOKDATEPUBLISHED));
BookIDs.add(parser.getValue(e, KEY_BOOKID));
BookCode.add(parser.getValue(e, KEY_BOOKCODE));
BookPageCount.add(parser.getValue(e, KEY_PAGE_COUNT));
try {
bookImageURL = new URL(BookCoverPhotos.get(tempCount));
} catch (MalformedURLException e1) {
// e1.printStackTrace();
}
try {
bitMapImage = BitmapFactory.decodeStream(bookImageURL
.openConnection().getInputStream());
bitmapArray.add(bitMapImage);
} catch (IOException e2) {
// e2.printStackTrace();
bitmapArray.add(imageNotFound);
}
publishProgress(i + 1);
}
} else {
publishProgress(5000);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressDialog.setMessage(values[0]
+ " Book(s) found \nPlease wait...");
if (values[0] == 5000) {
Toast.makeText(context,
"Rrequest Time out!\nNo or Slow Internet Connection!",
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
ImageAdapter adapter = new ImageAdapter(context, act);
gridView.setAdapter(adapter);
if (gridViewState != null) {
gridView.onRestoreInstanceState(gridViewState);
}
}
}
class ViewHolder {
TextView txt_BooksTitle;
ImageView img_BookCoverPhoto;
}
}
在这里你可以注意到,首先我从Asynctask填充我的gridview,然后我将gridview设置为无限列表器
gridView.setOnScrollListener(new EndlessScrollListener());
下次我从Endless listner类中调用asynctask。
new UIThread().execute(URL, initialIndex + "");
你必须从那里完成所有任务。我在无尽的列表中将这一行称为上述行。
答案 1 :(得分:0)
尝试这种方式:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { myListItems2 = new ArrayList<String>(); adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, myListItems2); View root = inflater.inflate(R.layout.tab_frag1_layout, container, false); lv = (ListView) root.findViewById(android.R.id.list); lv.setAdapter(adapter); return root; }
答案 2 :(得分:0)
Activity act = this.getActivity();
这作为成员变量将获得null,这将导致NPE
act.runOnUiThread(returnRes);
尝试在运行时询问活动,而不是成员变量:
getActivity().runOnUiThread(returnRes);