简体   繁体   中英

NullPointerException from fragment function when trying to update a textView from the main

I'm trying to create an app that lists cities in one fragment and when clicked, print a short description in another fragment.

Here is the description class with the updateDescription function:

public class Description extends Fragment {

public Description() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setRetainInstance(true);
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.description_fragment   , container, false);
}

public void updateDescription(String text){
    TextView textView = (TextView) getView().findViewById(R.id.text_to_be_updated);
    textView.setText(text);

}

}

And here is the .xml file for that class:

    <FrameLayout 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:id="@+id/des_layout"
tools:context="com.example.connormclean.lab3.Description">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="146dp"
    android:id="@+id/text_to_be_updated"
    android:textSize="25sp"
    android:layout_gravity="center"
    android:text="@string/frag_test"
    android:textAlignment="center"
    android:textColor="@android:color/black" />

This is causing this error:

  E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.connormclean.lab3, PID: 3903
              java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.connormclean.lab3.Description.updateDescription(java.lang.String)' on a null object reference
                  at com.example.connormclean.lab3.MainActivity.sendText(MainActivity.java:47)
                  at com.example.connormclean.lab3.City_list.getDescription(City_list.java:91)
                  at com.example.connormclean.lab3.City_list$1.onItemClick(City_list.java:46)
                  at android.widget.AdapterView.performItemClick(AdapterView.java:310)
                  at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
                  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
                  at android.widget.AbsListView$3.run(AbsListView.java:3879)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Any suggestions or solutions would be appreciated. Thanks!

The answer is pointed to by this line in the stack trace:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.connormclean.lab3.Description.updateDescription(java.lang.String)' on a null object reference
at com.example.connormclean.lab3.MainActivity.sendText(MainActivity.java:47)

The mistake is on line 47 of MainActivity , which you did not include in your question. The object on which you are invoking updateDescription() is null. Look for the problem there. You probably forgot to initialize it properly.

From where you are calling updateDescription(String text) method, call it once the view is created. If you call it before the view is created then you will get NullPointerException.

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