0

I've got following code, which gets launched as a second activity:

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



}

Here's my XML file:

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

I keep on getting a nullpointer exception:

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

All I did was really just copy past from my main activity, plus adjusting the values. The main activity works without any problems, this one however doesn't. I've tried cleaning, rebuilding the project, to no avail.

Can anyone please tell me whats wrong?

Thanks, Regards

18
  • while calling the adapter of an arraylist try to add android.R.id.text1 it has to be followed after the layout Commented Oct 3, 2013 at 19:58
  • Please post the entire stack trace. Commented Oct 3, 2013 at 19:59
  • How do I get the stack trace? Sorry I'm new at this. Commented Oct 3, 2013 at 20:01
  • You posted the first line of it. If you keep reading the lines, you'll see "caused by". Either paste all the lines down to "caused by" or the complete list. It will tell you exactly where the problem is. Commented Oct 3, 2013 at 20:02
  • 3
    Are you sure the xml you posted is from activity_sensor.xml? It looks like your ListView is null. And what is at line 24? Commented Oct 3, 2013 at 20:08

2 Answers 2

1

I've declared " final ListView listVals = (ListView) findViewById(R.id.listvalues);" as a field, so before running setContentView(). Once I've moved it into the actual function, it worked. So I assume that was causing it to return null?

Yes. If you call findViewById(R.id.someId); before calling setContentView(R.layout.someLayout); it will return null because you haven't yet inflated your layout which contains the View id you are trying to find.

Basically, your Views exist inside of your layout so if you try to use findViwById() before you inflate your layout (usually with setContentView()) then there is nothing to find. So you will get NPE when you try to use like when you call a function on it.

Sign up to request clarification or add additional context in comments.

Comments

0

If your project has external jar files, go to Project->properties->order and export and select all external jar files. That should do the work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.