小编Pis*_*gha的帖子

android片段在方向更改时创建了两次

我有这个奇怪的问题,我的列表片段创建两次,一次是在父活动上调用super.oncreate时,一次是在同一父活动上调用setContentView时.这是一个简单的应用程序,我使用不同的纵向和横向布局.

这是主要活动:

private HeadlinesFragment headlines;

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.w("MainActivity", "Before super.onCreate: " + this.toString());
    super.onCreate(savedInstanceState);
    Log.w("MainActivity", "Before setContentView: " + this.toString());
    setContentView(R.layout.news_articles);

    //check to see if its portrait
    if (findViewById(R.id.fragment_container) != null) {
        if(getSupportFragmentManager().findFragmentById(R.id.fragment_container) == null) {
            headlines = new HeadlinesFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, headlines).commit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是layout-land文件夹中的news_articles:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment android:name="com.example.android.fragments.HeadlinesFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

<fragment android:name="com.example.android.fragments.ArticleFragment"
          android:id="@+id/article_fragment"
          android:layout_weight="2"
          android:layout_width="0dp"
          android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

这是布局文件夹中的news_articles(纵向方向)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

这里是两次创建的headlinesfragment

public class HeadlinesFragment …
Run Code Online (Sandbox Code Playgroud)

android fragment android-orientation

13
推荐指数
1
解决办法
9545
查看次数

代码401,需要登录,同时在Android上使用YouTube Data API V3

我想在Android上使用新的Youtube Data API从给定的ID中检索视频标题,

https://www.googleapis.com/youtube/v3/videos?id=6DbS1VB8HGo&part=player&key=XXX

但是我一直得到一个回复​​说要求登录和错误代码401.

我猜测它与授权令牌有关,但我不明白为什么我需要这个,因为我没有执行任何帐户特定的操作,例如更新或从播放列表中删除.我想要的只是标题,也许是视图的数量.

应该感谢任何帮助,下面是我正在使用的代码:

public class NetTask extends AsyncTask<String, Void, String>{
    @Override
    protected String doInBackground(String... params) {
        HttpClient httpClient = new DefaultHttpClient();
        //param[0]= "https://www.googleapis.com/youtube/v3/videos?id=6DbS1VB8HGo&part=player&key=xxyy"
        HttpPost httpPost = new HttpPost(params[0]);
        HttpResponse response;

        try {
            response = httpClient.execute(httpPost);
            String responseString = EntityUtils.toString(response.getEntity());
            JSONObject responseJSON = new JSONObject(responseString);
            responseJSON.toString();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

java android youtube-api

6
推荐指数
1
解决办法
1658
查看次数