0

我有以下代码,它作为第二个活动启动:

public class SensorActivity extends Activity implements SensorEventListener{

List<Sensor> sensors;
Sensor selectedSens;
SensorManager SensMng;
ArrayAdapter<String> adapter;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sensor);

    final ListView listSensors = (ListView) findViewById(R.id.listvalues);


    SensMng = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensors = SensMng.getSensorList(Sensor.TYPE_ALL);
    ArrayAdapter<Sensor> adapter = new ArrayAdapter<Sensor>(this, android.R.layout.simple_list_item_1, sensors);
    listSensors.setAdapter(adapter);



}

这是我的 XML 文件:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listvalues"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

我不断收到一个空指针异常:

10-03 21:46:57.475: E/AndroidRuntime(9893): FATAL EXCEPTION: main
10-03 21:46:57.475: E/AndroidRuntime(9893): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ch.ethz.inf.vs.android.spurra.sensors/ch.ethz.inf.vs.android.spurra.sensors.SensorActivity}: java.lang.NullPointerException
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.os.Looper.loop(Looper.java:137)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.main(ActivityThread.java:4898)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at java.lang.reflect.Method.invoke(Method.java:511)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at dalvik.system.NativeStart.main(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893): **Caused by**: java.lang.NullPointerException
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.Activity.findViewById(Activity.java:1882)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at ch.ethz.inf.vs.android.spurra.sensors.SensorActivity.<init>(SensorActivity.java:24)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at java.lang.Class.newInstanceImpl(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at java.lang.Class.newInstance(Class.java:1319)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
10-03 21:46:57.475: E/AndroidRuntime(9893):     ... 11 more

我所做的只是从我的主要活动中复制过去,并调整值。主要活动没有任何问题,但是这个没有。我试过清理,重建项目,但无济于事。

谁能告诉我怎么了?

感谢和问候

4

2 回答 2

1

我已经声明了“最终 ListView listVals = (ListView) findViewById(R.id.listvalues);” 作为一个字段,所以在运行 setContentView() 之前。一旦我将它移到实际功能中,它就起作用了。所以我认为这导致它返回null?

是的。如果您在调用它findViewById(R.id.someId);之前调用setContentView(R.layout.someLayout);它,return null因为您尚未膨胀layout包含View id您要查找的内容的内容。

基本上,您Views存在于您的内部,因此如果您在充气之前layout尝试使用(通常使用),那么就什么也找不到了。因此,当您尝试使用 like 时,您会在其上调用函数。findViwById()layoutsetContentView()NPE

于 2013-10-03T20:54:34.693 回答
0

如果您的项目有外部 jar 文件,请转到 Project->properties->order 并导出并选择所有外部 jar 文件。那应该做的工作。

于 2013-10-03T20:11:12.077 回答