简体   繁体   English

尝试从主更新 textView 时片段函数中的 NullPointerException

[英]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:这是带有 updateDescription 函数的描述类:

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:这是该类的 .xml 文件:

    <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.错误在MainActivity第 47 行,您没有将其包含在您的问题中。 The object on which you are invoking updateDescription() is null.您调用updateDescription()的对象为空。 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.从您调用 updateDescription(String text) 方法的位置,在创建视图后调用它。 If you call it before the view is created then you will get NullPointerException.如果在创建视图之前调用它,则会得到 NullPointerException。

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

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