2

我想得到屏幕的压力。当我运行应用程序向我显示一条消息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
4

1 回答 1

8

压力是从 0 到 1 的浮点数。它的含义取决于硬件。即使有硬件压力传感器,实际力与返回值之间的映射getPressure()仍然取决于设备。此外,一些没有硬件压力传感器的设备会根据触摸点的大小来模拟压力,也就是说,它们假设更多的压力意味着你的手指会变平。其他设备只返回一个常量值。

此外,力或重量(以牛顿为单位)与质量(以克为单位)不同。请参阅http://en.wikipedia.org/wiki/Mass_versus_weight

如果您需要有关异常的帮助,那么您应该发布源代码和引发异常的行。adb log是你的朋友。

编辑:

您使用未初始化的压力。它只是声明但没有设置任何东西,所以它是null. 调用其方法之一是 NullPointerException。只有Views(小部件)可以接收MotionEvents。您需要对您的活动做一些事情(例如膨胀布局),然后为布局的其中一个视图设置一个触摸侦听器:

someView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float pressure = event.getPressure();
    }
});
于 2013-08-30T17:54:04.760 回答