为什么这个片段没有得到捆绑?

时间:2014-02-22 15:13:44

标签: java android android-fragments bundle

我正在制作一个简单的应用来学习使用片段。我想将一个字符串从一个活动转移到一个片段。我是由Bundle做的,因为它不起作用。在fragment类中,它不接受代码并将其突出显示为无法访问的代码。 这是我的代码: 主要活动:         公共类MainActivity扩展了FragmentActivity {

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

        // storing string resources into Array
        //
        setContentView(R.layout.activity_main);
        String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
        //List view
        final ListView list = (ListView)findViewById(R.id.questionsList);
        ArrayAdapter<String> adapter;
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, adobe_products);

         //final TextView text = (TextView)findViewById(R.id.textView1);

        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){

                String selectedFromList =(String) (list.getItemAtPosition(position));


                Fragment fragment = new Fragment();
                Bundle bundle = new Bundle();
                bundle.putString("key", selectedFromList);
                fragment.setArguments(bundle);

            }
        });

        list.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

主要活动XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/questionsList"
        android:layout_width="144dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.07" >

    </ListView>

    <fragment
        android:id="@+id/article_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="com.example.layout.MyFragment" />

</LinearLayout>

片段

    public class MyFragment extends Fragment {

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

        View myFragmentView =  inflater.inflate(R.layout.activity_fragment, container, false);

        Bundle bundle = getArguments();
        String selected = bundle.getString("key");

        TextView text = (TextView) myFragmentView.findViewById(R.id.text);
        text.setText(selected);


        // Inflate the layout for this fragment

        return myFragmentView;



    }
}

更新:我刚刚进行了更改,我没有此错误,但仍然无法打开应用。它在装载时停止。这是catLog:

    02-22 10:45:05.791: E/AndroidRuntime(1867): FATAL EXCEPTION: main
02-22 10:45:05.791: E/AndroidRuntime(1867): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layout/com.example.layout.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
02-22 10:45:05.791: E/AndroidRuntime(1867):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-22 10:45:05.791: E/AndroidRuntime(1867):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-22 10:45:05.791: E/AndroidRuntime(1867):     at android.app.ActivityThread.access$600(ActivityThread.java:141)

3 个答案:

答案 0 :(得分:0)

把返回放在底部,返回意味着我已经完成并完成了这个方法和片段。

 private TextView text;

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


        text = (TextView) myFragmentView.findViewById(R.id.text);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.activity_fragment, container, false);


    }

你的捆绑操作代码不起作用,你创建一个新的Fragment实例,但就是这样。应该做的是:

在MyFragment类中添加一个方法,例如

public void setMyText(String s){

        text.setText(s);
}

并在点击监听器中

 list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){

            String selectedFromList =(String) (list.getItemAtPosition(position));


               MyFragment f = (MyFragment) getSupportFragmentManager()
                      .findViewById(R.id.article_fragment);
               f.setMyText(selectedFromList);

        }
    });

答案 1 :(得分:0)

已经说明了返回问题,我想补充以下内容以便理解:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.activity_fragment,
            container, false);

           Intent intent = getActivity().getIntent();
           Bundle bundle = this.getArguments();
           String selected = bundle.getString("key");

    //You now are able to use myFragmentView like this: 
    //TextView textView = (TextView)myFragmentView.findViewById(R.id.someTextView);

   //at the end of the file:

  return myFragmentView;

}

这表示您在调用时已将Arguments放入Fragments包中,如下所示:

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("key", value);
fragment.setArguments(bundle);

再次编辑:由于您的片段是硬编码的,请参阅nr4bts版本。如果你从来没有把任何东西放在那里,你不能从捆绑中拿走任何东西。

答案 2 :(得分:0)

有些事情,您需要获取onClick之外的现有引用(进入onCreate),然后还需要以不同方式检索Bundle。

检查这个问题:

How to transfer some data to another Fragment?