gpt4 book ai didi

Android 在 Asynctask 之前检查网络连接

转载 作者:行者123 更新时间:2023-11-30 03:57:04 25 4
gpt4 key购买 nike

任何人都可以纠正我的代码。我收到错误“The method getApplicationContext() is undefined for the type FreeFragment”和“The method getSystemService(String) is undefined for the type FreeFragment`”。我希望它在 Asynctask 运行之前检查互联网连接。

public class FreeFragment extends SherlockListFragment implements
ActionBar.TabListener {

static final String URL = "https://myxml.xml";
static final String KEY_SONG = "song";
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_CAT_ARTIST = "artistcat";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_BIG_URL = "big_url";
static final String KEY_CAT_URL = "cat_url";
static final String KEY_DESC = "cat_desc";
ArrayList<HashMap<String, String>> menuItems;
ListAdapter adapter;

private Fragment mFragment;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
if (!isNetworkAvailable()) {
Toast.makeText(getApplicationContext(),
"Internet Connection Required", Toast.LENGTH_SHORT).show();
//finish(context);
} else {
new loadListView().execute();
}
super.onActivityCreated(savedInstanceState);
}


private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}

public class loadListView extends AsyncTask<Integer, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(Integer... args) {
// updating UI from Background Thread
menuItems = new ArrayList<HashMap<String, String>>();

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_CAT_ARTIST, parser.getValue(e, KEY_CAT_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
map.put(KEY_CAT_URL, parser.getValue(e, KEY_CAT_URL));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
return null;
}

@Override
protected void onPostExecute(String args) {

if (getActivity() != null) {
adapter = new MainPageLazyAdapter(getActivity(), menuItems);
setListAdapter(adapter);

ListView lv = getListView();

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String thumburl;
String bigurl;
String caturl;
String title;
String desc;
String artist;
Intent in;

title = ((TextView) view.findViewById(R.id.title))
.getText().toString();
artist = ((TextView) view.findViewById(R.id.artist))
.getText().toString();
desc = ((TextView) view.findViewById(R.id.desc))
.getText().toString();
thumburl = ((TextView) view
.findViewById(R.id.thumb_image)).getText()
.toString();
caturl = ((TextView) view.findViewById(R.id.cat))
.getText().toString();
bigurl = ((TextView) view.findViewById(R.id.big_image))
.getText().toString();

in = new Intent(getActivity(),
SingleImageViewActivity.class);

in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_DESC, desc);
in.putExtra(KEY_CAT_URL, caturl);
in.putExtra(KEY_THUMB_URL, thumburl);
in.putExtra(KEY_BIG_URL, bigurl);
in.putExtra(KEY_CAT_ARTIST, artist);
startActivity(in);

}
});
}

}
}



@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mFragment = new FreeFragment();
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}
}

最佳答案

使用FreeFragment.getActivity()方法访问您的 fragment 的 Activity ,然后获取上下文和系统服务

关于Android 在 Asynctask 之前检查网络连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13190511/

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