我一直致力于一个我想在世界杯开始之前完成的项目。 我一直在使用AndroidHive的SlidingMenu和RssReader。我也是Android编程的新手,并且在使用Fragments时遇到了很多麻烦。
基本上,我想使用每个片段的TextViews和Imageview显示足球组数据。我一直遇到的问题是应用一个实际打开Activity而不会崩溃的Button。之前在这个帖子中已经提出过这个问题:
How to start Activity from Fragment? Android
我一直试图实现asynctask,但我不能让它工作。 该代码与我发布的链接中描述的100%相同。
这是两个文件的Zip,有人可以帮帮我吗? http://www.file-upload.net/download-8963794/SlidingMenu.zip.html
提前感谢任何可以帮助我的人!
答案 0 :(得分:0)
只需执行以下操作即可使代码正常工作:
只需复制并粘贴如下。除此之外没什么可做的。 Lemme知道您是否需要对代码进行更多解释。
注意:要从片段启动活动,您需要使用getActivity()然后使用这样的意图,
Intent intent = new Intent(getActivity(),
AndroidRssReader.class);
getActivity().startActivity(intent);
1)只需在FindPeopleFragment中更改一下代码:
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people,
container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Button newPage = (Button) getActivity().findViewById(R.id.button1);
newPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),
AndroidRssReader.class);
getActivity().startActivity(intent);
}
});
}
}
2)将您的AndroidRssReader修改为:
public class AndroidRssReader extends ListActivity {
private List<String> item = new ArrayList<String>();
public static final String MAIN_URL = "http://drinkandtink.bugs3.com/covendos/Android-er%3Fformat%3Dxml";
Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
new BackgroundTask(context, MAIN_URL).execute();
}
public void getImpData(String URL) {
try {
URL rssUrl = new URL(URL);
SAXParserFactory mySAXParserFactory = SAXParserFactory
.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class BackgroundTask extends AsyncTask<Void, Void, Void> {
Context mcontext;
String someURL;
ProgressDialog pDialog;
public BackgroundTask(Context context, String SomeURL) {
super();
this.mcontext = context;
this.someURL = SomeURL;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Checking Network Connection");
pDialog.setIndeterminate(true);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
getImpData(someURL);
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
ArrayAdapter<String> itemList = new ArrayAdapter<String>(context,
R.layout.rsslist, item);
setListAdapter(itemList);
pDialog.dismiss();
}
}
private class RSSHandler extends DefaultHandler {
final int stateUnknown = 0;
final int stateTitle = 1;
int state = stateUnknown;
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("title")) {
state = stateTitle;
} else {
state = stateUnknown;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
state = stateUnknown;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String strCharacters = new String(ch, start, length);
if (state == stateTitle) {
item.add(strCharacters);
}
}
}
}
现在它的工作非常完美......我肯定会接受我对未来观众的回答..很享受......:D