我试图弄清楚单击按钮时如何保存加速度计读数。不幸的是,每次程序进入此类时,都会导致错误。我该如何解决这个问题?请帮忙。
package com.example.splashscreen;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.ImageButton;
public class Posms_2 extends Activity implements SensorEventListener {
Sensor accelerometer;
SensorManager sm;
TextView acceleration, set_acceleration;
ImageButton setIP;
float x_accel=0, y_accel=0, z_accel=0, a=0, b=0, c=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
setIP = (ImageButton)findViewById(R.id.imageButton3);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posms_2);
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
acceleration=(TextView)findViewById(R.id.textView1);
set_acceleration = (TextView)findViewById(R.id.textView2);
setIP.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
x_accel=a;
y_accel=b;
z_accel=c;
set_acceleration.setText("SAVED VALUES: \nX: "+x_accel+
"\nY: "+y_accel+
"\nZ: "+z_accel);
}
});
GoBack();
}
private void GoBack() {
ImageButton goback = (ImageButton) findViewById(R.id.imageButton1);
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
};
goback.setOnClickListener(myListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.posms_2, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent arg0) {
a=arg0.values[0];
b=arg0.values[1];
c=arg0.values[2];
acceleration.setText("Current Reading: \nX: "+arg0.values[0]+
"\nY: "+arg0.values[1]+
"\nZ: "+arg0.values[2]);
}
}
来自 LogCat
07-10 14:36:04.955: D/dalvikvm(125): GREF has increased to 201
07-10 14:36:05.575: D/skia(128): purging 27K from font cache [4 entries]
07-10 14:36:06.084: D/dalvikvm(128): GC_EXPLICIT freed 150K, 52% free 2715K/5639K, external 930K/1038K, paused 510ms
07-10 14:36:10.587: D/skia(151): purging 82K from font cache [11 entries]
07-10 14:36:10.845: D/dalvikvm(151): GC_EXPLICIT freed 269K, 50% free 2988K/5895K, external 2713K/3169K, paused 261ms
07-10 14:36:11.895: D/dalvikvm(189): GC_CONCURRENT freed 242K, 51% free 2796K/5703K, external 716K/1038K, paused 18ms+24ms
07-10 14:36:12.294: W/ActivityManager(67): No content provider found for:
07-10 14:36:12.365: D/PackageParser(67): Scanning package: /data/app/vmdl1450405817.tmp
07-10 14:36:15.605: D/skia(125): purging 6K from font cache [1 entries]
07-10 14:36:16.124: D/dalvikvm(125): GC_EXPLICIT freed 151K, 50% free 2925K/5767K, external 716K/1038K, paused 514ms
07-10 14:36:18.536: D/dalvikvm(151): GC_EXPLICIT freed 116K, 51% free 2924K/5895K, external 2194K/2357K, paused 279ms
07-10 14:36:19.194: D/dalvikvm(67): GC_CONCURRENT freed 641K, 45% free 4371K/7879K, external 1865K/2377K, paused 24ms+24ms
07-10 14:36:20.174: I/PackageManager(67): Removing non-system package:com.example.splashscreen
07-10 14:36:20.174: I/ActivityManager(67): Force stopping package com.example.splashscreen uid=10036
07-10 14:36:20.615: D/skia(67): purging 116K from font cache [13 entries]
07-10 14:36:20.994: D/dalvikvm(67): GC_EXPLICIT freed 475K, 45% free 4361K/7879K, external 1865K/2377K, paused 357ms
07-10 14:36:21.365: D/PackageManager(67): Scanning package com.example.splashscreen
07-10 14:36:21.365: I/PackageManager(67): Package com.example.splashscreen codePath changed from /data/app/com.example.splashscreen-1.apk to /data/app/com.example.splashscreen-2.apk; Retaining data and using new
07-10 14:36:21.377: I/PackageManager(67): Unpacking native libraries for /data/app/com.example.splashscreen-2.apk
07-10 14:36:21.575: D/installd(34): DexInv: --- BEGIN '/data/app/com.example.splashscreen-2.apk' ---
07-10 14:36:25.635: D/skia(121): purging 6K from font cache [1 entries]
07-10 14:36:26.144: D/dalvikvm(121): GC_EXPLICIT freed 145K, 49% free 3150K/6087K, external 716K/1038K, paused 495ms
07-10 14:36:26.545: D/dalvikvm(326): DexOpt: load 557ms, verify+opt 3312ms
07-10 14:36:26.675: D/installd(34): DexInv: --- END '/data/app/com.example.splashscreen-2.apk' (success) ---
07-10 14:36:26.675: W/PackageManager(67): Code path for pkg : com.example.splashscreen changing from /data/app/com.example.splashscreen-1.apk to /data/app/com.example.splashscreen-2.apk
07-10 14:36:26.685: W/PackageManager(67): Resource path for pkg : com.example.splashscreen changing from /data/app/com.example.splashscreen-1.apk to /data/app/com.example.splashscreen-2.apk
07-10 14:36:26.685: D/PackageManager(67): Activities: com.example.splashscreen.MainActivity com.example.splashscreen.TipsSection com.example.splashscreen.Tip1 com.example.splashscreen.Tip2 com.example.splashscreen.Tip3 com.example.splashscreen.Tip4 com.example.splashscreen.Tip5 com.example.splashscreen.Tip6 com.example.splashscreen.Tip7 com.example.splashscreen.Posms_1 com.example.splashscreen.Posms_2
07-10 14:36:26.695: I/ActivityManager(67): Force stopping package com.example.splashscreen uid=10036
07-10 14:36:27.084: I/installd(34): move /data/dalvik-cache/data@app@com.example.splashscreen-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.example.splashscreen-2.apk@classes.dex
07-10 14:36:27.094: D/PackageManager(67): New package installed in /data/app/com.example.splashscreen-2.apk
07-10 14:36:27.505: I/ActivityManager(67): Force stopping package com.example.splashscreen uid=10036
07-10 14:36:27.835: D/dalvikvm(151): GC_EXPLICIT freed 20K, 51% free 2904K/5895K, external 2061K/2357K, paused 233ms
07-10 14:36:28.284: I/ActivityManager(67): Start proc com.svox.pico for broadcast com.svox.pico/.VoiceDataInstallerReceiver: pid=327 uid=10009 gids={}
07-10 14:36:28.405: W/RecognitionManagerService(67): no available voice recognition services found
07-10 14:36:29.405: D/dalvikvm(189): GC_EXPLICIT freed 59K, 52% free 2749K/5703K, external 716K/1038K, paused 1725ms
07-10 14:36:29.765: I/ActivityThread(327): Pub com.svox.pico.providers.SettingsProvider: com.svox.pico.providers.SettingsProvider
07-10 14:36:30.885: D/dalvikvm(67): GC_EXPLICIT freed 514K, 46% free 4281K/7879K, external 1865K/2377K, paused 454ms
07-10 14:36:31.184: I/installd(34): unlink /data/dalvik-cache/data@app@com.example.splashscreen-1.apk@classes.dex
07-10 14:36:31.304: D/AndroidRuntime(210): Shutting down VM
07-10 14:36:31.344: I/AndroidRuntime(210): NOTE: attach of thread 'Binder Thread #3' failed
07-10 14:36:31.354: D/dalvikvm(210): GC_CONCURRENT freed 100K, 72% free 293K/1024K, external 0K/0K, paused 3ms+4ms
07-10 14:36:31.364: D/jdwp(210): adbd disconnected
07-10 14:36:33.665: D/AndroidRuntime(340): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-10 14:36:33.665: D/AndroidRuntime(340): CheckJNI is ON
07-10 14:36:36.375: D/AndroidRuntime(340): Calling main entry com.android.commands.am.Am
07-10 14:36:36.515: I/ActivityManager(67): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.splashscreen/.MainActivity } from pid 340
07-10 14:36:36.735: D/AndroidRuntime(340): Shutting down VM
07-10 14:36:36.785: I/AndroidRuntime(340): NOTE: attach of thread 'Binder Thread #3' failed
07-10 14:36:36.935: I/ActivityManager(67): Start proc com.example.splashscreen for activity com.example.splashscreen/.MainActivity: pid=349 uid=10036 gids={}
07-10 14:36:36.954: D/dalvikvm(340): GC_CONCURRENT freed 101K, 69% free 318K/1024K, external 0K/0K, paused 3ms+154ms
07-10 14:36:37.034: D/jdwp(340): adbd disconnected
07-10 14:36:41.875: I/ActivityManager(67): Displayed com.android.launcher/com.android.launcher2.Launcher: +2m34s14ms
07-10 14:36:42.194: D/dalvikvm(349): GC_EXTERNAL_ALLOC freed 52K, 53% free 2561K/5379K, external 716K/1038K, paused 1255ms
07-10 14:36:47.345: I/InputReader(67): Device reconfigured: id=0x0, name=qwerty2, display size is now 320x480
07-10 14:36:47.345: I/InputManager-Callbacks(67): No virtual keys found for device qwerty2.
07-10 14:36:48.354: I/ActivityManager(67): Displayed com.example.splashscreen/.MainActivity: +11s638ms
07-10 14:36:48.655: I/ARMAssembler(67): generated scanline__00000077:03515104_00001004_00000000 [ 65 ipp] (85 ins) at [0x43c68520:0x43c68674] in 3469438 ns
07-10 14:36:48.795: I/ARMAssembler(67): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x43c68678:0x43c68840] in 3669460 ns
07-10 14:36:49.325: I/ARMAssembler(67): generated scanline__00000177:03515104_00001002_00000000 [ 87 ipp] (110 ins) at [0x43c68848:0x43c68a00] in 3799506 ns
07-10 14:36:59.114: I/ActivityManager(67): Starting: Intent { cmp=com.example.splashscreen/.Posms_1 } from pid 349
07-10 14:36:59.595: D/dalvikvm(349): GC_EXTERNAL_ALLOC freed 12K, 52% free 2589K/5379K, external 1243K/1557K, paused 278ms
07-10 14:37:01.895: I/ActivityManager(67): Displayed com.example.splashscreen/.Posms_1: +2s726ms
07-10 14:37:04.974: I/ActivityManager(67): Starting: Intent { cmp=com.example.splashscreen/.Posms_2 } from pid 349
07-10 14:37:05.715: D/dalvikvm(349): GC_EXTERNAL_ALLOC freed 13K, 52% free 2620K/5379K, external 2070K/2109K, paused 277ms
07-10 14:37:06.084: D/dalvikvm(349): GC_EXTERNAL_ALLOC freed 2K, 52% free 2621K/5379K, external 1915K/2427K, paused 307ms
07-10 14:37:06.515: D/AndroidRuntime(349): Shutting down VM
07-10 14:37:06.515: W/dalvikvm(349): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-10 14:37:06.575: E/AndroidRuntime(349): FATAL EXCEPTION: main
07-10 14:37:06.575: E/AndroidRuntime(349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.splashscreen/com.example.splashscreen.Posms_2}: java.lang.NullPointerException
07-10 14:37:06.575: E/AndroidRuntime(349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-10 14:37:06.575: E/AndroidRuntime(349): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-10 14:37:06.575: E/AndroidRuntime(349): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-10 14:37:06.575: E/AndroidRuntime(349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-10 14:37:06.575: E/AndroidRuntime(349): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 14:37:06.575: E/AndroidRuntime(349): at android.os.Looper.loop(Looper.java:123)
07-10 14:37:06.575: E/AndroidRuntime(349): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-10 14:37:06.575: E/AndroidRuntime(349): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 14:37:06.575: E/AndroidRuntime(349): at java.lang.reflect.Method.invoke(Method.java:507)
07-10 14:37:06.575: E/AndroidRuntime(349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-10 14:37:06.575: E/AndroidRuntime(349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-10 14:37:06.575: E/AndroidRuntime(349): at dalvik.system.NativeStart.main(Native Method)
07-10 14:37:06.575: E/AndroidRuntime(349): Caused by: java.lang.NullPointerException
07-10 14:37:06.575: E/AndroidRuntime(349): at com.example.splashscreen.Posms_2.onCreate(Posms_2.java:32)
07-10 14:37:06.575: E/AndroidRuntime(349): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-10 14:37:06.575: E/AndroidRuntime(349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-10 14:37:06.575: E/AndroidRuntime(349): ... 11 more
07-10 14:37:06.645: W/ActivityManager(67): Force finishing activity com.example.splashscreen/.Posms_2
07-10 14:37:06.707: W/ActivityManager(67): Force finishing activity com.example.splashscreen/.Posms_1
07-10 14:37:07.214: W/ActivityManager(67): Activity pause timeout for HistoryRecord{406125f0 com.example.splashscreen/.Posms_2}
07-10 14:37:15.005: W/ActivityManager(67): Launch timeout has expired, giving up wake lock!
07-10 14:37:17.334: W/ActivityManager(67): Activity idle timeout for HistoryRecord{4053e040 com.example.splashscreen/.MainActivity}
07-10 14:37:22.777: W/ActivityManager(67): Activity destroy timeout for HistoryRecord{405b0248 com.example.splashscreen/.Posms_1}
07-10 14:37:22.785: W/ActivityManager(67): Activity destroy timeout for HistoryRecord{406125f0 com.example.splashscreen/.Posms_2}
07-10 14:41:01.964: D/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
07-10 14:42:06.754: I/Process(349): Sending signal. PID: 349 SIG: 9
07-10 14:42:06.914: I/ActivityManager(67): Process com.example.splashscreen (pid 349) has died.
07-10 14:42:06.924: E/InputDispatcher(67): channel '406c4720 com.example.splashscreen/com.example.splashscreen.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
07-10 14:42:06.924: E/InputDispatcher(67): channel '406c4720 com.example.splashscreen/com.example.splashscreen.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
07-10 14:42:07.005: I/WindowManager(67): WIN DEATH: Window{406c4720 com.example.splashscreen/com.example.splashscreen.MainActivity paused=false}
07-10 14:42:07.166: I/WindowManager(67): WIN DEATH: Window{406e5d20 com.example.splashscreen/com.example.splashscreen.Posms_1 paused=true}
07-10 14:42:07.495: E/InputDispatcher(67): Received spurious receive callback for unknown input channel. fd=128, events=0x8
07-10 14:42:07.684: W/InputManagerService(67): Got RemoteException sending setActive(false) notification to pid 349 uid 10036