I want to get the pressure of the screen. When ill run the application displaying me a message Unfortunately, Application has stopped.
I have another question when ill get the pressure of the screen does its possible to convert in a weigh (grams) with some math or physic ? Here is the code i have:
EDIT: I'm running this application on tablet
package com.realscale.stefanrafa;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textinfo;
MotionEvent pressure;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textinfo = (TextView) findViewById(R.id.Information);
float press = pressure.getPressure();
textinfo.setText("Scale: " + (int) press);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Error logs :
08-30 19:53:40.620: W/ActivityThread(29458): Application com.realscale.stefanrafa can be debugged on port 8100... 08-30 19:53:40.710: D/AndroidRuntime(29458): Shutting down VM 08-30 19:53:40.710: W/dalvikvm(29458): threadid=1: thread exiting with uncaught exception (group=0x41a0c2a0)
08-30 19:53:40.715: E/AndroidRuntime(29458): FATAL EXCEPTION: main 08-30 19:53:40.715: E/AndroidRuntime(29458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.realscale.stefanrafa/com.realscale.stefanrafa.MainActivity}: java.lang.NullPointerException
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.os.Looper.loop(Looper.java:137)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.app.ActivityThread.main(ActivityThread.java:4921)
08-30 19:53:40.715: E/AndroidRuntime(29458): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 19:53:40.715: E/AndroidRuntime(29458): at java.lang.reflect.Method.invoke(Method.java:511)
08-30 19:53:40.715: E/AndroidRuntime(29458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 08-30 19:53:40.715: E/AndroidRuntime(29458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-30 19:53:40.715: E/AndroidRuntime(29458): at dalvik.system.NativeStart.main(Native Method)
08-30 19:53:40.715: E/AndroidRuntime(29458): Caused by: java.lang.NullPointerException
08-30 19:53:40.715: E/AndroidRuntime(29458): at com.realscale.stefanrafa.MainActivity.onCreate(MainActivity.java:19)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.app.Activity.performCreate(Activity.java:5188)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-30 19:53:40.715: E/AndroidRuntime(29458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-30 19:53:40.715: E/AndroidRuntime(29458): ... 11 more
Pressure is a float from 0 to 1. What it means is hardware-dependent. Even if there is a hardware pressure sensor, the map between the actual force and the return value of getPressure()
still depends on the device. Also, some devices without a hardware pressure sensor simulate pressure from the size of the touch point, that is, they are assuming that more pressure means your finger flattens out. Other devices just return a constant value.
Also, force or weight (measured in Newtons) is not the same as mass (measured in grams). See http://en.wikipedia.org/wiki/Mass_versus_weight.
If you want help with your exception then you should post the source and the line where it is thrown. adb log
is your friend.
EDIT:
You use pressure uninitialized. Its just declared but not set to anything, so it is null
. Calling one of its methods is a NullPointerException. Only View
s (widgets) can receive MotionEvent
s. You need to do something with your activity (e.g. inflate a layout) and then set a touch listener to one of the views of the layout:
someView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float pressure = event.getPressure();
}
});