解: 这是一个具有2个不同WinnersFragment实例的情况,它导致lv = getListView()在刷新时为null。
我的片段列表视图在加载时工作正常。但是当我调用public static void InitTask(String searchState)时。我还没有在lv = getListView()创建内容视图。
我的目标是用新的json刷新listview。
我尝试在执行前调用getlistView,并尝试使用新的inittask 我相信它与创建WinnersFragment的新实例有关,但我不确定。
我发现了这个"Content view not yet Created" on Android Fragments但是我理解它在主线程中遇到了我不明白如何实现解决方案以确定这是否是我的问题。
public class WinnersFragment extends ListFragment {
private static String URL = "http://www.xxx.com/RichardsTesting/winners.json";
Context context;
Winners_List_Adapter adapter;
static View v;
ListView lv;
ArrayList<String> alFName;
private ProgressDialog pd;
private static final String TAG_ID = "id";
private static final String TAG_FNAME = "fname";
private static final String TAG_STATE = "state";
private static final String TAG_CODE = "code";
private static final String TAG_AMOUNT = "amount";
ArrayList<HashMap<String, String>> winnersList;
JSONArray winners = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.winners_list, container, false);
context = getActivity().getApplicationContext();
new InitTask().execute();
return v;
}
@Override
public void onDestroy() {
lv.setAdapter(null);
super.onDestroy();
}
protected class InitTask extends AsyncTask<Context, Integer, String> {
@Override
protected void onPreExecute() {
pd = new ProgressDialog(v.getContext());
pd.setTitle("Updating...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected String doInBackground(Context... params) {
// Hashmap for ListView
winnersList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
String json = jParser.getJSONFromUrl(URL);
try {
JSONArray json1 = new JSONArray(json);
// looping through All Contacts
for (int i = 0; i < json.length(); i++) {
JSONObject c = json1.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String fName = c.getString(TAG_FNAME);
String state = c.getString(TAG_STATE);
String code = c.getString(TAG_CODE);
String amount = c.getString(TAG_AMOUNT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_FNAME, fName);
map.put(TAG_STATE, state);
map.put(TAG_CODE, code);
map.put(TAG_AMOUNT, amount);
// adding HashList to ArrayList
winnersList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return "COMPLETE!";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// selecting single ListView item
lv = getListView();
if (pd != null) {
pd.dismiss();
}
setListWinners();
}
}
public void setListWinners() {
adapter = new Winners_List_Adapter(this.getActivity(), winnersList);
lv.setAdapter(adapter);
}
public static void InitTask(String searchState) {
if (searchState !=null) {
URL = URL+"?state="+searchState;
WinnersFragment search = new WinnersFragment();
search.new InitTask().execute();
}
}
}
LOG
12-04 13:01:48.156: E/AndroidRuntime(1367): FATAL EXCEPTION: main
12-04 13:01:48.156: E/AndroidRuntime(1367): java.lang.IllegalStateException: Content view not yet created
12-04 13:01:48.156: E/AndroidRuntime(1367): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
12-04 13:01:48.156: E/AndroidRuntime(1367): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
12-04 13:01:48.156: E/AndroidRuntime(1367): at com.xxx.xxx.WinnersFragment$InitTask.onPostExecute(WinnersFragment.java:123)
12-04 13:01:48.156: E/AndroidRuntime(1367): at com.xxx.xxx.WinnersFragment$InitTask.onPostExecute(WinnersFragment.java:1)
12-04 13:01:48.156: E/AndroidRuntime(1367): at android.os.AsyncTask.finish(AsyncTask.java:631)
12-04 13:01:48.156: E/AndroidRuntime(1367): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-04 13:01:48.156: E/AndroidRuntime(1367): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-04 13:01:48.156: E/AndroidRuntime(1367): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 13:01:48.156: E/AndroidRuntime(1367): at android.os.Looper.loop(Looper.java:137)
12-04 13:01:48.156: E/AndroidRuntime(1367): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-04 13:01:48.156: E/AndroidRuntime(1367): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 13:01:48.156: E/AndroidRuntime(1367): at java.lang.reflect.Method.invoke(Method.java:511)
12-04 13:01:48.156: E/AndroidRuntime(1367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
12-04 13:01:48.156: E/AndroidRuntime(1367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
12-04 13:01:48.156: E/AndroidRuntime(1367): at dalvik.system.NativeStart.main(Native Method)
XML
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
调用片段代码
public class TabsFragmentActivity extends FragmentActivity implements
FragmentTabHost.OnTabChangeListener, OnClickListener {
blablabla
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnDismiss:
getSupportFragmentManager().popBackStackImmediate();
break;
case R.id.ibSearch:
LayoutInflater inflater = LayoutInflater.from(this);
final View textenter = inflater.inflate(R.layout.winners_list, null);
final EditText userinput = (EditText) textenter
.findViewById(R.id.etSearchState);
String searchState = userinput.toString();
WinnersFragment.InitTask(searchState);
break;
}
}
答案 0 :(得分:1)
尝试在onActivityCreated()方法中执行后台任务。
答案 1 :(得分:0)
如错误消息所示,您的ListView视图尚未创建。 (您在onCreateView中调用它,即使该方法异步运行,您也可以在完全设置之前尝试访问ListView。)
将InitTask()。execute()移动到onViewCreated
或onActivityCreated
。
答案 2 :(得分:0)
onCreateView
在返回此视图之前,当您尝试访问listview时,返回为此片段显示的视图,您将收到该错误。
尝试执行@Makario所说的内容,或者通过以下方式获取onCreateView中的列表视图引用:
v = inflater.inflate(R.layout.winners_list, container, false);
context = getActivity().getApplicationContext();
listView=v.findViewById(R.id.list);
new InitTask().execute();
在任务中使用此listview对象。