Android app crash after setText on a TextView

丶灬走出姿态 提交于 2021-01-29 09:12:16

问题


I'm trying to set the text of a TextView programmatically. In my app I'm calling a method which creates a TableRow with aView inside. This View contains a TextView. And now I'm trying to change the Text with the method 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"/>
...

and my fragment 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;
}

So if I try to run this on my phone the app just closed and logcat give me this

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.access$800(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

I don't know what's wrong with the setText line, can someone tell me?

Maybe helpful: In my fragment I'm pointing on another layout (fragment_layout). But I want to create a View of city_layout and change the text in the textview. This view I want to display in fragment_layout.


回答1:


Here's your problem

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() here is null.

change it to:

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

and you'll be all set.




回答2:


instead

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

try

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


来源:https://stackoverflow.com/questions/28004886/android-app-crash-after-settext-on-a-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!