Android的碎片getArguments()返回null碎片、Android、null、getArguments

2023-09-05 02:16:55 作者:没有印记

正如标题所暗示。 我从这里, http://developer.android.com下载的片段,code /shareables/training/FragmentBasics.zip 。 距离的Andr​​oid开发者官方网站片段的例子。 http://developer.android.com/training/basics/fragments/fragment -ui.html

As the title suggest. I've downloaded fragment code from here, http://developer.android.com/shareables/training/FragmentBasics.zip. It is Fragment example from Android Official Developer site. http://developer.android.com/training/basics/fragments/fragment-ui.html

这是MainActivity.java的onCreate:

This is the MainActivity.java's onCreate:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_articles);

    // Check whether the activity is using the layout version with
    // the fragment_container FrameLayout. If so, we must add the first fragment
    if (findViewById(R.id.fragment_container) != null) {

        // However, if we're being restored from a previous state,
        // then we don't need to do anything and should return or else
        // we could end up with overlapping fragments.
        if (savedInstanceState != null) {
            return;
        }

        // Create an instance of ExampleFragment
        HeadlinesFragment fragment = new HeadlinesFragment();

        // In case this activity was started with special instructions from an Intent,
        // pass the Intent's extras to the fragment as arguments
        //fragment.setArguments(getIntent().getExtras());
        Bundle args= new Bundle();
        args.putString("category", "clothes");
        args.putString("item", "shirts");
        fragment.setArguments(args);

        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment).commit();
    }
}

和HeadlinesFragment.java的onCreate:

And HeadlinesFragment.java's onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // We need to use a different list item layout for devices older than Honeycomb
    int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
            android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

    Bundle args = getArguments();
    if (args == null) {
        Toast.makeText(getActivity(), "arguments is null " , Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getActivity(), "text " + args , Toast.LENGTH_LONG).show();
    }        

    // Create an array adapter for the list view, using the Ipsum headlines array
    setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));

}

我在这里看了几QA,像这样的片段getArguments()返回null ,以及相关的setArguments()和getArguments(),但我依然坚持。很多其他

I've read several QA here, like this one Fragment getArguments() returns null, and many other that related to setArguments() and getArguments(), but still I'm stuck.

无果而我搬到了包和吐司 code到onAttach()和onCreateView()。有什么毛病我的code?我想我失去了一些东西,但不知道它是什么。 请帮忙!谢谢你。

And I've moved the Bundle and Toast code to onAttach() and to onCreateView() with no avail. What wrong with the my code? I think I'm missing something, but dunno what is it. Please Help! Thanks.

修改: 我会说出我的意图更加清晰。在FragmentBasic我下载了,有MainActivity.java,HeadlinesFragment.java和ArticlesF​​ragment.java。在沟通从MainActivity.java到ArticlesF​​ragment.java是不是这里的问题。我想是从MainActivity.java数据发送到HeadlinesFragment.java。它们的连接是这样的:

Edit: I'll state my intention more clearly. In FragmentBasic that I downloaded, there's MainActivity.java, HeadlinesFragment.java, and ArticlesFragment.java. The 'communication' from MainActivity.java to ArticlesFragment.java is not the problem here. What I want is to send data from MainActivity.java to HeadlinesFragment.java. Their connection's like this:

--------------------------------------
| MainActivity <-> HeadlinesFragment |
|      |                             |
|      |>> ArticlesFragment          |
--------------------------------------

和HeadlinesFragment在运行时运行。

And HeadlinesFragment is running at Runtime.

*这些code ++工程,使用以&lt Android的小工具时; 600px的宽度。在平板电脑上使用,但不工作(> = 600px的),作为证明@ Tesla1984以下。但我要的是无论是在小工具&LT相同的结果; 600px的和小工具> 600px的。

*These code works, when using Android gadget with < 600px width. But doesn't work when using on tablet ( >= 600px), As proved by @Tesla1984 below. But what I want is same result either on gadget < 600px and on gadget > 600px.

推荐答案

我已经解决了。貌似从MainActivity.java数据发送到HeadlinesFragment.java的唯一方式是从回调(如果任何人知道的其他方式,请贡献,那么我们还有一些其他的方法这一个旁边,帮助他人有这样那样的问题)。

I've solved it. Looks like the only way to send data from MainActivity.java to HeadlinesFragment.java is from callbacks (If anybody else know other ways, please contribute, then we have some other ways beside this one, helping others with this kind of problem).

从主code是MainActivity.java的功能公开捆绑的getBundle(){} ,然后设置接口上HeadlinesFragment.java部分,并添加公开捆绑的getBundle(); ,和最后,从HeadlinesFragment.java的的onCreate 。

The main code from is MainActivity.java's function public Bundle getBundle() {}, then set the interface section on HeadlinesFragment.java, and add public Bundle getBundle();, and last, call it from HeadlinesFragment.java's onCreate.

什么让我困惑是 fragment.setArguments(getIntent()getExtras()); 在MainActivity.java的的onCreate 。他们把那个code那里,我相信它会工作的原因,而是从 Android的官方开发指南和API 的 http://developer.android.com/training/basics/fragments/fragment-ui.html ,但它没有工作(现在我相信那块code什么都不会做)。那么,谁看了从那里的教程或样品,把它与一粒盐!

What confuse me is fragment.setArguments(getIntent().getExtras()); on MainActivity.java's onCreate. They put that code there, and I believing it will works cause it's from Android Official Developer Guide and API http://developer.android.com/training/basics/fragments/fragment-ui.html, but it didn't work (now I believe that piece of code won't do anything). So, anyone who read the tutorial or sample from there, take it with a grain of salt!

codeS下面,让每个人都能理解它。

Codes below, so everyone can understand it.

MainActivity.java

    /*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.fragments;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

public class MainActivity extends FragmentActivity 
        implements HeadlinesFragment.OnHeadlineSelectedListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            Toast.makeText(getApplicationContext(), "activity", Toast.LENGTH_LONG).show();

            // Create an instance of ExampleFragment
            HeadlinesFragment fragment = new HeadlinesFragment();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            fragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, fragment).commit();
        }
    }

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment

        // Capture the article fragment from the activity layout
        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);

        } else {
            // If the frag is not available, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }

    public Bundle getBundle() {
        Bundle args = new Bundle();
        args.putString("category", "cloths");
        args.putString("item", "shirts");
        return args;
    }
}

HeadlinesFragment.java

    /*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.fragments;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // The container Activity must implement this interface so the frag can deliver messages
    public interface OnHeadlineSelectedListener {
        /** Called by HeadlinesFragment when a list item is selected */
        public void onArticleSelected(int position);
        public Bundle getBundle();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = mCallback.getBundle();
        Toast.makeText(getActivity(), "headline fragment " + bundle, Toast.LENGTH_LONG).show();

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

        // Create an array adapter for the list view, using the Ipsum headlines array
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
                    savedInstanceState) {

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item
        mCallback.onArticleSelected(position);

        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }
}