使用ListView时出现NullPointerexception

时间:2013-10-03 19:56:27

标签: android listview nullpointerexception

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

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" />

我继续得到一个nullpointer异常:

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

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

有谁能告诉我什么错了?

谢谢, 此致

2 个答案:

答案 0 :(得分:1)

  

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

是。如果您在致电findViewById(R.id.someId);之前致电setContentView(R.layout.someLayout);,则会return null,因为您还没有夸大包含您要查找的layout的{​​{1}}。

基本上,View id存在于Views内部,因此如果您在layout(通常是findViwById())充气之前尝试使用layout,那么没有什么可以找的。因此,当您在调用函数时尝试使用时,您将获得setContentView()

答案 1 :(得分:0)

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