简体   繁体   English

将值从MainActivity传递到扩展Fragment的Java_Class失败

[英]Passing values from MainActivity to a Java_Class that extends Fragment fails

To more understand more how fragments work, I created a mainactivity its layout has one <fragment> . 为了进一步了解fragments如何工作,我创建了一个mainactivity其布局具有一个<fragment> and I created two java classes extend fragment and each of these classes has its own layout. 并且我创建了两个java类扩展fragment ,每个这些类都有自己的布局。

In the java class that extends fragment , I initailise my views textview inside onActivityCreated() , and as shown below in the code in the same class, i created two methods setbtnclicks(int clicks) and getbtnclicks() . 在扩展fragment的java类中,我在onActivityCreated()实现了自己的view textview扩展,并且如以下同一类代码中所示,我创建了两个方法setbtnclicks(int clicks)getbtnclicks() From the mainactivity i assign number of clicks of a button to setbtnclicks() and i try to display the number of clicks on the textview of the class that extends fragment by calling getbtnclicks from inside onActivivtyCreated() . mainactivity我将按钮的点击次数分配给setbtnclicks()然后尝试通过从onActivivtyCreated()内部调用getbtnclicks来显示扩展fragment的类的textview的点击次数。 But the problem is, this method always displays zero, as if the number of clicks are not incremented. 但是问题是,此方法始终显示为零,就像单击次数没有增加一样。

MainActivtity: MainActivtity:

//global variable
private int i = 0;
...
...
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Fragment mSelectedFragment;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

    switch (v.getId()) {
    case R.id.btn00:    
        mSelectedFragment = new Fragment00();
        mFragmentManager = getFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.fragment00ID, mSelectedFragment);
        mFragmentTransaction.commit();
        mFragment00.setBtnClicks(i);
        i++;
        break;

Java_Class "Fragment00" Java_Class“ Fragment00”

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    Log.d(TAG, "onActivityCreated(): "+order);
    order++;
    mTv = (TextView) getView().findViewById(R.id.fragment00Tv00);
    mTv.setText("the Button was clicked "+getBtnClicks()+ " time(s)");
    Log.i(TAG, "onActivityCreated(): "+getBtnClicks());
}

increment i before the fragment Transaction 在片段事务之前增加i

switch (v.getId()) {
case R.id.btn00:  
     i++;  
    mSelectedFragment = new Fragment00();
    mFragmentManager = getFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.fragment00ID, mSelectedFragment);
    mFragmentTransaction.commit();
    mFragment00.setBtnClicks(i);

    break;

Once i have implemented a more or less same program like yours.The objective was to increment the number of button clicks in both the fragments.Here is the code.Hope it helps: 一旦我实现了一个与您大致相同的程序,目标是增加两个片段中的按钮单击次数,这是代码,希望它会有所帮助:

MainActivity.java MainActivity.java

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;


public class MainActivity extends Activity implements OnClickListener{
Button btn1,btn2;
int click1=0,click2=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }
    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Fragment fragment = null;
         if (v == btn1) {
                // do stuff for button left
             click1++;

             Bundle bundle=new Bundle();
                String clickstring1=Integer.toString(click1);
                 bundle.putString("name1",clickstring1 );

             fragment = new FragButton1();


                    fragment.setArguments(bundle);


            }
         if (v == btn2) {
                // do stuff for button right
             click2++;
             Bundle bundle=new Bundle();
                String clickstring2=Integer.toString(click2);
             bundle.putString("name2",clickstring2 );
                fragment = new FragButton2();
                fragment.setArguments(bundle);
            }

         if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();
         }
         else{
             Log.e("MainActivity", "Error in creating fragment");
         }

    }



}

FragButton1.java FragButton1.java

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class FragButton1 extends Fragment{

    TextView tv;
public FragButton1(){}

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

        View rootView = inflater.inflate(R.layout.fragbutton1, container, false);
        String strtext=getArguments().getString("name1");
        tv=(TextView)rootView.findViewById(R.id.tv_times1);
        tv.setText(strtext);

        return rootView;
    }

}

FragButton2.java FragButton2.java

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("NewApi")
public class FragButton2 extends Fragment{

    TextView tv2;
public FragButton2(){}

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

        View rootView = inflater.inflate(R.layout.fragbutton2, container, false);
        tv2=(TextView)rootView.findViewById(R.id.tv_times2);
        String strtext=getArguments().getString("name2");
        tv2.setText(strtext);
        return rootView;
    }


}

Here there are 2 buttons and onclick of a button a new fragment will be launched and the fragment will display the number of times a button is clicked in a textview. 这里有2个按钮,单击按钮时将启动一个新片段,该片段将显示在文本视图中单击按钮的次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM