Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6729323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:16:33+00:00 2026-05-26T10:16:33+00:00

I’m writing an app to display the values of the accelerometer in Android in

  • 0

I’m writing an app to display the values of the accelerometer in Android in the X, Y, and Z directions. I am an Android noob so please bear with me. Here is my code, it is quite simple but I receive a null pointer exception and I’m not sure what’s wrong with my code. Thanks!

package com.*****.accelo;

import android.app.Activity;

import android.content.Context;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.widget.TextView;



public class AcceloActivity extends Activity implements SensorEventListener{



TextView textViewX, textViewY, textViewZ, textViewError;

StringBuilder builderx = new StringBuilder();
StringBuilder buildery = new StringBuilder();
StringBuilder builderz = new StringBuilder();




public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);

    textViewError = (TextView) findViewById(R.id.textViewError);
    textViewX = (TextView) findViewById(R.id.textViewX);
    textViewY = (TextView) findViewById(R.id.textViewY);
    textViewX = (TextView) findViewById(R.id.textViewZ);




    SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    Sensor accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        if(!manager.registerListener(this, accelerometer,  
SensorManager.SENSOR_DELAY_UI)){
        setContentView(textViewError);

        }
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {



}



public void onSensorChanged(SensorEvent event) {

    builderx.setLength(0);
    buildery.setLength(0);
    builderz.setLength(0);

    builderx.append("X " + event.values[0]);
    buildery.append("Y"+  event.values[1] );
    builderz.append("Z " + event.values[2]);

    textViewX.setText(builderx.toString());
    textViewY.setText(buildery.toString());
    textViewZ.setText(builderz.toString());

}



}

From logcat:

10-24 19:52:43.574: ERROR/AndroidRuntime(711): java.lang.RuntimeException: Unable to   
start activity ComponentInfo{com.amrit.accelo/com.amrit.accelo.AcceloActivity}: 
java.lang.NullPointerException
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at  
android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.os.Handler.dispatchMessage(Handler.java:99)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at 
android.os.Looper.loop(Looper.java:123)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
android.app.ActivityThread.main(ActivityThread.java:3683)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at      
java.lang.reflect.Method.invokeNative(Native Method)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at    
java.lang.reflect.Method.invoke(Method.java:507)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
dalvik.system.NativeStart.main(Native Method)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): Caused by: java.lang.NullPointerException      
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.amrit.accelo.AcceloActivity.onCreate(AcceloActivity.java:50)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:16:34+00:00Added an answer on May 26, 2026 at 10:16 am

    You are getting this error from line 50:

    textViewError.setText("Error, could not register sensor listener");

    so your textViewError variable is null here. You will need to create an XML layout, use setContentView, and then add a TextView to your XML layout and then assign your TextView to it with findViewById. This is probably a good place to start.

    EDIT:

    Just to be clear, you need to set content view before you call findViewById.

    public void onCreate(Bundle savedInstanceState){
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.yourlayout);
    
        textViewError = (TextView) findViewById(R.id.textViewError);
        textViewX = (TextView) findViewById(R.id.textViewX);
        textViewY = (TextView) findViewById(R.id.textViewY);
        textViewX = (TextView) findViewById(R.id.textViewZ);
        ...
    

    Important here is that you must have an xml layout file called yourlayout.xml under res/layout/ which contains the views that you are loading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.