我想得到屏幕的压力。当运行不当的应用程序向我显示消息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到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();
    }
});

关于android - 如何在Android上检测屏幕压力?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18538513/

10-12 03:41