-1

I'm trying to do adding text inside the fragment, but whenever i call a function: updateTextField Errors are seen although he is everything is will, but do not know the reform of the problem often tried to , Is it possible amendment to some of the codes to correct the error please help me codes: // MainActivity

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>

// Logcat:

  Process: com.example.android.settextfromoutfragment, PID: 2536
                                                                                          java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                              at android.view.View.performClick(View.java:5076)
                                                                                              at android.view.View$PerformClick.run(View.java:20279)
                                                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                              at android.os.Looper.loop(Looper.java:135)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5910)
                                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                                           Caused by: java.lang.reflect.InvocationTargetException
                                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                              at android.view.View.performClick(View.java:5076) 
                                                                                              at android.view.View$PerformClick.run(View.java:20279) 
                                                                                              at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                              at android.os.Looper.loop(Looper.java:135) 
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5910) 
                                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
                                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                              at com.example.android.settextfromoutfragment.SecondFragment.updateTextField(SecondFragment.java:27)
                                                                                              at com.example.android.settextfromoutfragment.MainActivity.test_btn(MainActivity.java:36)
                                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                                              at android.view.View.performClick(View.java:5076) 
                                                                                              at android.view.View$PerformClick.run(View.java:20279) 
                                                                                              at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                              at android.os.Looper.loop(Looper.java:135) 
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5910) 
                                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
ahmed hassn
  • 73
  • 1
  • 5

2 Answers2

0

You can pass the string in the second fragment by argument and onCreateView method, you can retrieve this argument and set on your text view. Here is the sample code.

Add below code on your onClick method.

 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;
}

Retrieve your String in onCreateView method of your Fragment.

Bundle args = getArguments();
String myString = args.getString("myString");

And, set this string to your textview

 updateText.setText(myString);
VP4Android
  • 576
  • 4
  • 17
0

hi you can not access change text in MainActivity , you can do this as two way :

1 :

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);
    }
}

2 : EventBus : and you learn more click this

hamid
  • 171
  • 1
  • 1
  • 9
  • thanks for you , but cannot use :new SecondFragment.newInstance("asdas"); , i use SecondFragment.newInstance("asdas"); ,without new – ahmed hassn Dec 14 '18 at 21:39