Android 在 TextView 上设置文本后应用程序崩溃

Android app crash after setText on a TextView

我正在尝试以编程方式设置 TextView 的文本。在我的应用程序中,我调用了一个方法,该方法创建了一个 TableRow 和一个 View 内部。这个 View 包含一个 TextView。现在我正在尝试使用 setText().

方法更改文本

city_layout.xml

...
 <TextView
        android:id="@+id/textView_City_Name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/city_name"
        android:textColor="#FFFFFF"
        android:textSize="35dp"
        android:gravity="center_horizontal"
        android:textStyle="bold"
        android:paddingTop="65dp"/>
...

和我的片段class

private View createCityInList(String cityName) {
    TableRow tr = new TableRow(getActivity());
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.city_layout, tr, false);

    //change attr here!
    TextView textView = (TextView) getView().findViewById(R.id.textView_City_Name);
    textView.setText(cityName);
    return v;
}

因此,如果我尝试 运行 在我的 phone 上执行此操作,应用程序刚刚关闭并且 logcat 给我此操作

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.app.city/de.app.city.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access0(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException

我不知道 setText 行有什么问题,有人可以告诉我吗?

可能有帮助: 在我的片段中,我指向另一个布局 (fragment_layout)。但是我想创建一个 city_layout 的视图并更改文本视图中的文本。我想在 fragment_layout.

中显示此视图

这是你的问题

private View createCityInList(String cityName) {
    TableRow tr = new TableRow(getActivity());
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.city_layout, tr, false);

    //change attr here!
    TextView textView = (TextView) getView().findViewById(R.id.textView_City_Name);
    textView.setText(cityName);
    return v;
}

这里的getView()为空

改为:

private View createCityInList(String cityName) {
    TableRow tr = new TableRow(getActivity());
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.city_layout, tr, false);

    //change attr here!
    TextView textView = (TextView) v.findViewById(R.id.textView_City_Name);
    textView.setText(cityName);
    return v;
}

一切就绪。

改为

TextView textView = (TextView) getView().findViewById(R.id.textView_City_Name);

尝试

TextView textView = (TextView) v.findViewById(R.id.textView_City_Name);