在TextView上执行setText后,Android应用程序崩溃 [英] Android app crash after setText on a TextView
问题描述
我正在尝试以编程方式设置 TextView
的文本.在我的应用程序中,我正在调用一个方法,该方法创建一个内部带有 View
的 TableRow
.此 View
包含一个 TextView
.现在,我正在尝试使用 setText()
方法更改Text.
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
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"/>
...
和我的片段类
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;
}
因此,如果我尝试在手机上运行该应用程序,则该应用程序刚刚关闭,logcat给了我
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
我不知道setText行有什么问题,有人可以告诉我吗?
I don't know what's wrong with the setText line, can someone tell me?
可能有帮助:在我的片段中,我指向了另一个布局(fragment_layout).但是我想创建一个city_layout视图并更改textview中的文本.我要在fragment_layout中显示此视图.
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.
推荐答案
这是您的问题
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()在这里为空.
getView() here is null.
将其更改为:
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上执行setText后,Android应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!