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