我想要获得屏幕的压力。生病时,应用程序向我显示一条消息Unfortunately, Application has stopped.
我有另一个问题,当生病得到屏幕的压力可以转换成一个数学或物理的权重(克)?这是我的代码:
编辑:我在平板电脑上运行此应用程序
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;
}
}
错误日志:
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
答案 0 :(得分:6)
压力是从0到1的浮点数。这意味着与硬件有关。即使存在硬件压力传感器,实际力与getPressure()
的返回值之间的映射仍取决于设备。此外,一些没有硬件压力传感器的设备可以模拟触摸点大小的压力,也就是说,它们假设压力越大意味着手指变平。其他设备只返回一个恒定值。
此外,力或重量(以牛顿为单位)与质量(以克为单位测量)不同。请参阅http://en.wikipedia.org/wiki/Mass_versus_weight。
如果您需要有关异常的帮助,那么您应该发布源和引发它的行。 adb log
是你的朋友。
编辑:
你使用未初始化的压力。它刚刚声明但没有设置为任何东西,所以它是null
。调用其中一个方法是NullPointerException。只有View
个(小部件)才能收到MotionEvent
个。您需要对您的活动执行某些操作(例如,对布局进行充气),然后将触摸侦听器设置为布局的其中一个视图:
someView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float pressure = event.getPressure();
}
});