Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从YouTube应用程序导航回活动时使用FC_Android - Fatal编程技术网

Android 从YouTube应用程序导航回活动时使用FC

Android 从YouTube应用程序导航回活动时使用FC,android,Android,我有一个项目列表,单击该列表会启动一个带有youtube视频url的视频,然后启动youtube应用程序 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ return (inflater.inflate(R.layout.results, container, false)); } @Override public v

我有一个项目列表,单击该列表会启动一个带有youtube视频url的视频,然后启动youtube应用程序

@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 view, int pressed) {
current = results.get(pressed);
displayVideoPage();
}

public ArrayList searchYoutube(字符串搜索){
//收养自
// http://stackoverflow.com/questions/4023058/android-trying-to-get-data-from-youtube-api
网址;
// http://gdata.youtube.com/feeds/mobile/videos/-/{http://gdata.youtube.com/schemas/2007/keywords.cat}谷歌/{http://gdata.youtube.com/schemas/2007/keywords.cat}开发者
//呃,通常会使用正则表达式,但在关键时刻
String[]searchWords=search.split(“”);
字符串搜索URL=”http://gdata.youtube.com/feeds/mobile/videos/-";// /{http://gdata.youtube.com/schemas/2007/keywords.cat}
搜索URL=”http://gdata.youtube.com/feeds/api/videos?q=“+搜索词[0];
//http://gdata.youtube.com/feeds/api/videos?q=football+-足球和秩序=相关性和启动指数=11&最大结果=10&v=2
布尔值优先=真;
for(字符串s:搜索词){
如果(第一){
第一个=假;
}
否则{
searchUrl+=“+”+s;
}
//搜索URL+=”/{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”;
//搜索URL=”http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-指数=1,最大结果=15,v=2”;
System.out.println(searchUrl);
ArrayList输出=新的ArrayList();
试一试{
url=新url(搜索url);
URL连接;
connection=url.openConnection();
HttpURLConnection httpConnection=(HttpURLConnection)连接;
int responseCode=httpConnection.getResponseCode();
System.out.println(“响应代码:“+responseCode”);
if(responseCode==HttpURLConnection.HTTP\u确定){
InputStream in=httpConnection.getInputStream();
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
//解析地震提要。
文档dom=db.parse(in);
Element docEle=dom.getDocumentElement();
//获取每个地震条目的列表。
NodeList nl=docEle.getElementsByTagName(“条目”);
如果(nl!=null&&nl.getLength()>0){
final int length=nl.getLength();
for(int i=0;i
唯一的问题是当用户按下后退按钮退出Youtube应用程序并返回活动时。它通过变量搜索强制关闭一个null指针异常

我能做些什么来解决这个问题


我试着把它捆起来,然后把它拿出来,但没有成功。顺便说一句,我使用的是片段。

您可以查看startActivityForResult(意图)

然后您可以在中重新加载搜索或从共享优先级中提取 onActivityResult(int请求代码、int结果代码、意图数据)

编辑:一些代码

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);
下面是来自

onActivityResult()在onResume()之前调用,它也可以用来处理您的问题

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;



    }
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);
public class MyActivity extends Activity {
     ...

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