当我尝试在下面的代码中填充'lightName'TextView时,这可以正常工作,但是当我尝试使用'pressureName'时,我的应用程序崩溃了
public class ViewSensorData extends ActionBarActivity {
TextView lightName;
TextView pressureName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_data);
lightName = (TextView) findViewById(R.id.sensor_name_1);
lightName.setText(R.string.display_light);
pressureName = (TextView) findViewById(R.id.sensor_name_4);
pressureName.setText(R.string.display_pressure);
}
}
activity_sensor_data.xml文件看起来像是
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mycompany.myfirstapp.ViewSensorData">
<TextView
android:layout_width="211dp"
android:layout_height="wrap_content"
android:id="@+id/sensor_name_1"
android:layout_row="1"
android:layout_column="1" />
<TextView
android:layout_width="211dp"
android:layout_height="wrap_content"
android:id="@+id/sensor_name_4"
android:layout_row="4"
android:layout_column="1" />
</GridLayout>
最后,我的logcat看起来像这样
02-19 23:44:39.071 2685-2685/com.mycompany.myfirstapp I/PersonaManager﹕ getPersonaService() name persona_policy
02-19 23:44:39.101 2685-2685/com.mycompany.myfirstapp D/AndroidRuntime﹕ Shutting down VM
02-19 23:44:39.101 2685-2685/com.mycompany.myfirstapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418afda0)
02-19 23:44:39.101 2685-2685/com.mycompany.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mycompany.myfirstapp, PID: 2685
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myfirstapp/com.mycompany.myfirstapp.ViewSensorData}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mycompany.myfirstapp.ViewSensorData.onCreate(ViewSensorData.java:32)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
02-19 23:49:39.173 2685-2685/com.mycompany.myfirstapp I/Process﹕ Sending signal. PID: 2685 SIG: 9
答案 0 :(得分:0)
您应该通过其ID来获取资源,而不是设置它
lightName.setText(getResources().getString(R.string.display_light));
答案 1 :(得分:0)
在这里羞愧我的头。我已将最小SDK更改为14,而后者又生成了另一个activity_sensor_data.xml(v14)
文件。编辑此文件后,pressureName
TextView会按预期填充。