开发者

FC when navigated back to activity from YouTube app

I have a list of items that when clicked launches a video with the url to a youtube video, and the Youtube app is then launched.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return (inflater.inflate(R.layout.results, container, false));
}

@Override
public void onActivityCreated(Bundle state){
    super.onActivityCreated(state);

    search = AccessibleYouTube_Fragment.search;




        ((TextView) getView().findViewById(R.id.textViewSearchResultTitle))
        .setText("Captioned search results for " + search);
ArrayList<YouTubeResult> ytResults = searchYoutube(search);
int size = ytResults.size();
//lv.setAdapter(new ArrayAdapter<String>(this, R.layout., COUNTRIES))
ArrayList<String> ytResultsStr = new ArrayList<String>();
for(YouTubeResult result : ytResults) {
    ytResultsStr.add(result.toString());
}
results = ytResults;
//ListAdapter la = ;
((ListView) getView().findViewById(R.id.listViewSearchResults)).setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, ytResultsStr));
((ListView) getView().findViewById(R.id.listViewSearchResults)).requestFocus();
((ListView) getView().findViewById(R.id.listViewSearchResults)).setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        listViewClick(arg1, arg2);
    }

    });
    }


public void listViewClick(View 开发者_Go百科view, int pressed) {
current = results.get(pressed);
displayVideoPage();

}

public void displayVideoPage() {

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(current.url));

startActivity(i);

}

public ArrayList<YouTubeResult> searchYoutube(String search) {
// adopted from
// http://stackoverflow.com/questions/4023058/android-trying-to-get-data-from-youtube-api
URL url;
// http://gdata.youtube.com/feeds/mobile/videos/-/{http://gdata.youtube.com/schemas/2007/keywords.cat}google/{http://gdata.youtube.com/schemas/2007/keywords.cat}developers
//ugh, normally would use regular expression, but in big time crunch
String[] searchWords = search.split(" ");
String searchUrl = "http://gdata.youtube.com/feeds/mobile/videos/-";// /{http://gdata.youtube.com/schemas/2007/keywords.cat}
searchUrl = "http://gdata.youtube.com/feeds/api/videos?q=" + searchWords[0];
//http://gdata.youtube.com/feeds/api/videos?q=football+-soccer&orderby=relevance&start-index=11&max-results=10&v=2
boolean first = true;
for (String s : searchWords) {
    if(first) {
        first = false;
    }
    else {
        searchUrl += "+" + s;
    }
    //searchUrl += "/{http://gdata.youtube.com/schemas/2007/keywords.cat}" + s;

}
//searchUrl += "&caption&orderby=relevance&start-index=1&max-results=100&v=2";
searchUrl += "&orderby=relevance&v=2";
//searchUrl = "http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=1&max-results=15&v=2";
System.out.println(searchUrl);
ArrayList<YouTubeResult> output = new ArrayList<YouTubeResult>();
try {
    url = new URL(searchUrl);

    URLConnection connection;
    connection = url.openConnection();

    HttpURLConnection httpConnection = (HttpURLConnection) connection;

    int responseCode = httpConnection.getResponseCode();
    System.out.println("Response code:" + responseCode);
    if (responseCode == HttpURLConnection.HTTP_OK) {

        InputStream in = httpConnection.getInputStream();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        // Parse the earthquake feed.
        Document dom = db.parse(in);
        Element docEle = dom.getDocumentElement();

        // Get a list of each earthquake entry.
        NodeList nl = docEle.getElementsByTagName("entry");
        if (nl != null && nl.getLength() > 0) {
            final int length = nl.getLength();
            for (int i = 0; i < length; i++) {
                Element entry = (Element) nl.item(i);

                Element title = (Element) entry.getElementsByTagName("title").item(0);
                Element id = (Element) entry.getElementsByTagName("id").item(0);
                Element author = (Element) entry.getElementsByTagName("author").item(0);
                author = (Element) entry.getElementsByTagName("name").item(0);

                String titleStr = title.getFirstChild().getNodeValue();
                String authorStr = author.getFirstChild().getNodeValue();
                //tag:youtube.com,2008:video:2TCLeIyBwoU
                 videoUrl = "http://www.youtube.com/watch?v=" + id.getFirstChild().getNodeValue().split(":video:")[1];



                YouTubeResult ytr = new YouTubeResult(titleStr, videoUrl, authorStr);
                //ytr.title = titleStr;
                //ytr.url = videoUrl;
                //ytr.author = authorStr;
                //System.out.println(entry.toString());
                output.add(ytr);
                //System.out.println(ytr);
                // VideoCell cell = new VideoCell(titleStr);

                // Process a newly found earthquake
                // addVideoCellToArray(cell);
            }
        }
    }
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (SAXException e) {
    e.printStackTrace();
} finally {
}
return output;



    }

The only problem is when the user pressed the back button to exit the Youtube app and go back into the Activity. It force closes giving a NullPointer exception with the Variable Search.

What can i do to fix this?

Ive tried saving it in a bundle and then pulling it out, but no luck. I am using Fragment BTW.


You might look into startActivityForResult(Intent)

Then you could reload Search or pull from shared prefrences in onActivityResult(int requestCode, int resultCode, Intent data)

Edit: some code

public void displayVideoPage() {

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(current.url));

//The 1 is just passed by to you so you can identify what result is coming back
startActivityForResult(i, 1);

Below is from Android Developers

public class MyActivity extends Activity {
     ...

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == 1) {
             // Rebuild your search or whatever ...
             }
         }
     }
 }

onActivityResult() is called right before onResume(), which could also be used to handle your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜