Skip to content
Advertisement

Android Sensor return null?

The Android Java app that I am writing has a simple principle of operation: it reads data from sensors and displays it in a TextView. The first version of the application worked without any problems, but I wanted to improve it and work on the design. I decided to add a slideout side menu and that’s when the problems started. When trying to start in the emulator, an error was thrown that the application was stopped. There was a bug in the logcat (below) above related to TexView that wasn’t there.

MainActivity.java

JavaScript

fragment_acc.xml where I try pass value

JavaScript

LogCat Error

JavaScript

I see this is the problem in line 155:

JavaScript

It seems sensorEvent return null. The question is why? The first version of the app without additional fragments work perfectly.

Advertisement

Answer

First, you need to look at the error.

JavaScript

It means that you called the method ON a null thing, not WITH a null parameter. Therefore the problem is the TextView xAccValue, which is null.

From what I’ve seen, it looks like you put the TextView in a Fragment, in that case you need to reference (and set its text) from the Fragment‘s class.

You cannot reference Views in your Fragment from MainActivity.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement