【问题标题】:How to pass value from activity to Tab Fragment using replace Fragment?如何使用替换片段将值从活动传递到选项卡片段?
【发布时间】:2017-12-05 17:33:15
【问题描述】:

我有 activity ,我从中获取一个值并将其传递给 Tab Fragment ..在Tab Fragment中,我想用这个值来setText

问题是如何将这个值带到 Tab Fragment 中? 我总是得到这个异常 java.lang.IllegalArgumentException: No view found for id ....for fragment Tab1 .. 如何解决?

我在android studio中使用了普通的Tab活动并修改了这个方法

homee.java

public Fragment getItem(int position) {
        switch (position){
            case 0:
                Tab1 tab1 =new Tab1();
                return tab1;
            case 1:
                Tab2 tab2 =new Tab2();
                return tab2;
            case 2:
                Tab3 tab3 =new Tab3();
                return tab3;
        }
        return null ;
    }

这是我的活动编号.java

public class number extends AppCompatActivity {
Button add,save;
TextView num;
int n;
String stringNum;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_number);

    add =  findViewById(R.id.idadd);
    save =  findViewById(R.id.idsave);
    num = findViewById(R.id.idnum);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //simple method
            n = Integer.parseInt(num.getText().toString());
            num.setText(++n +"");
        }
    });

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stringNum= num.getText().toString(); 
// I want to pass this value to Tab1 Fragment by replaceFragment or by any 
//way
            Bundle bundle = new Bundle();
            bundle.putString("key", stringNum);
            Tab1 myFragment = new Tab1();
            myFragment.setArguments(bundle);


     getSupportFragmentManager().beginTransaction().replace
     (R.id.container,myFragment).commit();
            finish();
        }
    });
}}

这是 Tab 活动的 Tab1 子项

public class Tab1 extends Fragment {

Button get,update;
TextView txt;

public Tab1(){}

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

    get = view.findViewById(R.id.idget);
    update = view.findViewById(R.id.idup);
    txt = view.findViewById(R.id.textView);

    get.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(getContext(), number.class);
            startActivity(in);
        }
    });

    update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String Number= getArguments().getString("key").toString();
            //here the problem
            txt.setText(Number);
        }
    });

        return view;
        }
    }

activity_homee.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="eg.noname.opo.homee">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="@string/app_name">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_1" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_2" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_3" />

    </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fra">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="111dp"
    android:text="TextView"
    android:textAlignment="center"
    android:textSize="30sp" />

<Button
    android:id="@+id/idget"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="88dp"
    android:text="get" />

<Button
    android:id="@+id/idup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="update" />
</LinearLayout>

编辑我通过调用返回此值的静态方法解决了它,但我仍然应该按下片段中的按钮以将值更新为新值,我正在尝试找到一种更新方法当我从我的活动中传递它时自动传递该值..有什么帮助吗?

【问题讨论】:

  • 你能解释一下前两行问题吗?你想问什么?
  • @AnkurKhandelwal 好的,我编辑了它.. 现在清楚了吗?
  • 调试这一行 stringNum= num.getText().toString(); 并检查是否听到你得到一个值..?

标签: java android android-fragments android-tablayout


【解决方案1】:

在活动号.java中

  in onCreate method just call
  String Number= getArguments().getString("key").toString();
        //here the problem
        txt.setText(Number)

【讨论】:

  • 但我仍然无法通过 Bundle 接收值“Number”.. 我总是收到此异常 java.lang.IllegalArgumentException: No view found for id ....for fragment Tab1
【解决方案2】:

您只需在 getItem() 方法中调用 Fragment 时传递值。

public Fragment getItem(int position) {
    switch (position){
        case 0:
              Tab1 tab1= new Tab1();
              Bundle bundle = new Bundle();
              bundle.putString("name", name);
              tab1.setArguments(bundle);
              return tab1;
        case 1:
            Tab2 tab2= new Tab2();
            Bundle bundle = new Bundle();
            bundle.putString("mobile", mobile);
            tab2.setArguments(bundle);
            return tab2;
        case 2:
            Tab3 tab3= new Tab3();
            Bundle bundle = new Bundle();
            bundle.putString("email", email);
            tab3.setArguments(bundle);
            return tab3;
    }
    return null ;
}

您可以通过以下方式在下一个片段中检查它:

if( getArguments() != null) {
        name = getArguments().getString("name");

希望对你有帮助。

【讨论】:

    【解决方案3】:

    只需将以下捆绑包与您想要传递数据的片段一起传递:

    Bundle data = new Bundle();//create bundle instance
    data.putString("key_value", "String to pass");//put string to pass with key 
    fragmentName.setArguments(data);//Set bundle data to fragment
    

    您可以在下一个片段中使用以下代码检索数据:

     String getArgument = getArguments().getString("data");
     text.setText(getArgument); //set string over textview
    

    【讨论】:

    • 因为在 FragmentAdapter 中它会生成没有传递值的第二个 TAB 片段的新对象......如果是普通片段,这个答案是正确的..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多