【问题标题】:Pass values from fragment to fragment on Same Activity在同一活动上将值从片段传递到片段
【发布时间】:2014-06-11 08:07:06
【问题描述】:

在同一活动中将数字从一个片段传递到另一个片段时遇到问题,我将该数字传递给了 Activity,但是当我尝试将它传递给第二个片段时,它给了我 null。 任何帮助表示赞赏

这是传递的代码,还有更多,可能有错别字,因为我想尽可能少地输入代码

MainActivity

package com.example.design.ex;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import example.test.R;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.design.ex.fragments.ReceiptItemsFragment;


public class NewReceiptActivity extends SherlockFragmentActivity {

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


       int num = 0;

       Intent intent = getIntent();
       int Broj = intent.getIntExtra("Num", num);


     Bundle bundle=new Bundle();
    bundle.putInt("Broj", Broj);

         setContentView(R.layout.activity_test);

         ReceiptItemsFragment frag = (ReceiptItemsFragment) 
getSupportFragmentManager().findFragmentById(R.id.receipt);
         frag.setArguments(bundle);


     }


 }

片段 1

package com.example.design.ex.fragments;

import example.test.R;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.actionbarsherlock.app.SherlockFragment;

public class CategoriesItemsFragment extends SherlockFragment  {


int num = 0;

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

@Override
 public void onViewCreated(View view, Bundle savedInstanceState) {
     super.onViewCreated(view, savedInstanceState);
     init();
 }

 private void init() {


     itemsGrid.setOnItemClickListener(new OnItemClickListener() 
    {
         @Override
        public void onItemClick(AdapterView<?> arg0, final View itemView, int 
 arg2, long arg3)
         {
               num = arg2 + 1;


Intent intent = new Intent(getActivity().getBaseContext(),NewReceiptActivity.class);


          intent.putExtra("Num", num);
     startActivity(new Intent(getActivity(), NewReceiptActivity.class));   
          getActivity().startActivity(intent);


        }

     });


  }
}

片段 2

package example.design.ex.fragments;

import example.test.R;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;




public class ReceiptItemsFragment extends SherlockFragment {

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

    setHasOptionsMenu(true);



     Bundle arguments = getArguments();
     if (arguments != null)
    {
        Log.d("ISnull","no!");
     } else {
         Log.d("ISnull","yes!");
     }


 int num=getArguments().getInt("Num");


     return inflater.inflate(R.layout.fragment_receipt_items, container,
             false);


 }

主要活动 XML

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

<fragment
    android:id="@+id/titles"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1.5"
    class="example.ex.fragments.CategoriesItemsFragment" />

<LinearLayout
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@color/sun_flower" >
</LinearLayout>

<fragment
    android:id="@+id/receipt"
    android:name="example.ex.fragments.ReceiptItemsFragment"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="example.ex.fragments.ReceiptItemsFragment" />

</LinearLayout>

这是错误

 06-11 07:41:00.419: E/AndroidRuntime(2801):    at      
 com.example.NewReceiptActivity.onCreate(NewReceiptActivity.java:60)

如果我输入日志,这是这一行或下一行(试图让它向我显示 id)

ReceiptItemsFragment frag = (ReceiptItemsFragment)    
getSupportFragmentManager().findFragmentById(R.id.receipt); 

【问题讨论】:

  • 我得到 Null 而不是数字 2
  • 将值从片段传递到片段?。但是您正在尝试做的是从活动传递到片段。
  • 我正在从片段做片段......但我卡在 Activity 到片段。
  • 您的问题已解决或仍在等待中?
  • 待定.. 尝试@Guian 的解决方案

标签: android android-fragments


【解决方案1】:

验证NewReceiptActivity 中的frag 是否为不同的空值。验证Numt 是否为2

移动 ReceiptItemsFragment 中的行:

int num=getArguments().getInt("Num"); //this should get number 2 from Frist Fragment but it gives null

从方法 onCreateView 到 onActivityCreated:

public void onActivityCreated (Bundle savedInstanceState) {
    int num=getArguments().getInt("Num"); //this should get number 2 from Frist Fragment but it gives null

【讨论】:

  • 我一开始是 0(或者我输入 num 的任何数字),后来它变成了 2,我会尝试将它移动到 onActivityCreated
  • @MePo 请在您的问题中添加更多代码。你的应用程序的处理顺序是什么,谁是第一,第二,第三?谁叫谁?
  • 我想了解更多关于您的应用程序调用逻辑和顺序的信息。
  • 当然……它快把我逼疯了。
  • 为困扰我的解决方案稍微扩展了代码(添加了重要部分)。
【解决方案2】:

我认为问题出在这里:

 ReceiptItemsFragment frag=new ReceiptItemsFragment(); //2nd fragment

 frag.setArguments(bundle);

您正在创建一个新片段,但它不是您布局中的片段。

试试这个:

setContentView(R.layout.activity_test); // Here is where that 2 fragments are Located

ReceiptItemsFragment frag = getFragmentManager().findFragmentById(...) //your fragment id

frag.setArguments(bundle);

因此您无需创建新片段,而是引用布局中已有的片段。

希望对您有所帮助。

【讨论】:

  • Fragment frag = getFragmentManager().findFragmentById(R.id.receipt) 这给了我 null... 我将包含该片段的 xml
  • 您可能需要使用支持片段管理器(如果您的片段使用支持库),请参阅:stackoverflow.com/questions/16967328/…
  • 支持库比android.app中的支持库维护得更好,因此建议使用它们。
  • 事实上,我认为 Sherlock 依赖于 Support Library,因此是的,您需要 Support FragmentManager。
  • @Zhuinden 我这样做了,但同样的事情我得到了 null
【解决方案3】:

您可以创建 Application 的子类并将对象存储在其中。检查this。 或者您可以在活动类中使用静态变量。

【讨论】:

  • 我会尝试这种方式......如果我不能让它按照我想要的方式工作,谢谢你的建议
【解决方案4】:

我在传递数据时使用接口采用了不同的方法,如下所述

https://stackoverflow.com/a/12311939/1393695

一切顺利,感谢大家的帮助。

问题是我正在查看片段,因为它们在哪里活动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多