Java 无法从外部片段将文本放入textview?

Java 无法从外部片段将文本放入textview?,java,android,Java,Android,我试图在片段内添加文本,但每当我调用函数:updateTextField时都会看到错误,虽然他是万事如意,但不知道经常尝试的问题是什么,有没有可能修改一些代码来纠正错误 请帮帮我 代码: //主要活动 public class MainActivity extends AppCompatActivity { private android.view.View mEditText; @Override protected void onCreate(Bundle sa

我试图在片段内添加文本,但每当我调用函数:updateTextField时都会看到错误,虽然他是万事如意,但不知道经常尝试的问题是什么,有没有可能修改一些代码来纠正错误 请帮帮我 代码: //主要活动

public class MainActivity extends AppCompatActivity {


    private android.view.View mEditText;

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

    }


    public void test_btn(View V){

        SecondFragment newGamefragment = new SecondFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment2 , newGamefragment);
        transaction.addToBackStack(null);
        transaction.commit();
        newGamefragment.updateTextField("asdas");


    }



}

//SecondFragment



public class SecondFragment extends Fragment {

    private TextView updateText;

    public SecondFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        updateText = (TextView)view.findViewById(R.id.text_update);
        return view;
    }


    public void updateTextField(String newText){
        updateText.setText(newText);
    }
}

// xml mainactivity

<RelativeLayout 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:background="#574706"
    tools:context=".MainActivity">



    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/tv_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HIHIH"
        android:textSize="30dp"
        android:textColor="#54ffff"
        android:layout_below="@+id/fragment2"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="94dp"
        android:layout_marginTop="39dp" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_main"
        android:onClick="test_btn"

        />

</RelativeLayout>


// xml fragment_second

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="h"
        android:textColor="#ff0000"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:layout_marginTop="32dp" />

</RelativeLayout>

您可以通过参数和onCreateView方法在第二个片段中传递
字符串
,您可以检索此参数并在文本视图中进行设置。下面是示例代码

onClick
方法上添加以下代码

 SecondFragment newGamefragment = SecondFragment.newInstance();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment2 , newGamefragment);
        transaction.addToBackStack(null);
        transaction.commit();


/**
 * Create a new instance of secondFragment, 
 */
public static SecondFragment newInstance(String yourString) {
    secondFragment f = new secondFragment();
    Bundle args = new Bundle();
    args.putStirng("myString", yourString);
    f.setArguments(args);
    return f;
}
在片段的
onCreateView
方法中检索字符串

Bundle args = getArguments();
String myString = args.getString("myString");
然后,将此字符串设置为textview

 updateText.setText(myString);

您好,您无法访问MainActivity中的更改文本,您可以通过两种方式执行此操作:

1:

2:EventBus:
您将了解更多信息

Logcat中的错误是什么?使用
setOnClickListener(…)
方法设置
View.OnClickListener
。请参见:谢谢,但不能使用:new SecondFragment.newInstance(“asdas”),我使用SecondFragment.newInstance(“asdas”),没有新的
 updateText.setText(myString);
public class MainActivity extends AppCompatActivity {


    private android.view.View mEditText;

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

    }


    public void test_btn(View V){

        SecondFragment newGamefragment = new SecondFragment.newInstance("asdas");
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment2 , newGamefragment);
        transaction.addToBackStack(null);
        transaction.commit();



    }



}

//SecondFragment



public class SecondFragment extends Fragment {

    private TextView updateText;


    public static SecondFragment newInstance(String str) {
        SecondFragment s= new SecondFragment ();
        Bundle args = new Bundle();
        args.putString("str", str);
        signUpFragment.setArguments(args);
        return s;
    }

    public SecondFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        updateText = (TextView)view.findViewById(R.id.text_update);
        if (getArguments() != null)
            updateTextField( getArguments().getString("str"));
        return view;
    }


    public void updateTextField(String newText){
        updateText.setText(newText);
    }
}