简体   繁体   中英

changing the setText value in a fragment from main activity

I've been trying to change my textViews text in a fragmentActivity based on whether or not user has clicked the button in the first main activity. I have 2 xml files and 2 .java files and the code at the moment crashes when the app starts. This is the button code

public void getNum(View view) {

        buttonCheck = 1;
        }

And this is the code from FragmentTab

public class FragmentTab extends Fragment {

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

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

    TextView tv = (TextView) v.findViewById(R.id.text);
    tv.setText("nothing has been input yet");

    if (FirstActivity.buttonCheck == 1){
    tv = (TextView) v.findViewById(R.id.text);
    tv.setText("value");}


    FirstActivity.buttonCheck = 0;
    return v;
}

The first error i get from my code from logcat is: "java.lang.NullPointerException at com.ezentertainment.dietabialkowa.FragmentTab.onCreateView(FragmentTab.java:23)" and line 23 is tv.setText("nothing has been input yet"); any help at all is greatly appreciated, I have been fighting with this issue for quite some time now..

tl;dr how to change fragment value based upon input from mainActivity?

edit: here is the fragment_layout.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:orientation="vertical"
android:background="#eaecee">

<TextView
    android:id="@+id/textResult"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:text="value"
    android:textAppearance="?android:attr/textAppearanceMedium" />

Your textview id is

android:id="@+id/textResult"

So, that's why you're getting a null pointer exception.

Also, it's probably a good idea to do a null check when finding a view by ID, just in case something isn't instantiated yet, so it doesn't kill your app. Something like:

TextView tv = (TextView) v.findViewById(R.id.textResult);
if (tv != null){
     tv.setText("nothing has been input yet");
} else {
    // log something if you want.
}

How about using setArguement :

In Activity:

 Bundle bundle = new Bundle();
 bundle.putString("email", email);
 FragmentA fragment = new FragmentA();
 fragment.setArguments(bundle);
  getSupportFragmentManager().beginTransaction().add(container, fragment,     tag).addToBackStack(tag).commit();     //please input container and tag yourself

In FragmentA:

    String email;
       @Override
       public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle arguments = getArguments();
    if (arguments != null)
       email = bundle.getString("email");

    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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