我是Android的新手,并关注新的波士顿android系列,并被困在这里。 在传感器管理应用程序中获取空指针异常并且应用程序在我的课程中崩溃加速。 在真实设备上进行测试时。
我的代码:
package com.ss;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
public class Accelerate extends Activity implements SensorEventListener {
float x, y, sensorX, sensorY;
Bitmap ball;
SensorManager sm;
MyBringBackSurface ourSurfaceView;
@Override
protected void onPause() {
// TODO Auto-generated method stub
sm.unregisterListener(this);
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent e) {
try {
Thread.sleep(16);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sensorX = e.values[0]; // return sensor events of x axis
sensorY = e.values[1];
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
try {
setContentView(ourSurfaceView);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
ball = BitmapFactory
.decodeResource(getResources(), R.drawable.ball);
x = y = sensorX = sensorY = 0;
super.onCreate(savedInstanceState);
setContentView(new MyBringBackSurface(this));
ourSurfaceView.resume();
} catch (Exception e) {
Dialog d = new Dialog(this);
d.setTitle("ERROR");
TextView tv = new TextView(this);
String error = "IDHAR AYA HAI "+e.toString();
tv.setText(error);
d.setContentView(tv);
d.show();
}
}
public class MyBringBackSurface extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
Boolean isRunning = false;
public MyBringBackSurface(Context context) {
super(context);
ourHolder = getHolder();
}
public void pause() {
// TODO Auto-generated method stub
isRunning = false;
while (true) {
Log.v("message", "Exceptino not");
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.v("message", "Exceptino", e);
}
break;
}
ourThread = null;
}
public void resume() {
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
Log.v("message", "started thread");
}
@Override
public void run() {
Log.v("messge", isRunning.toString());
try {
Canvas canvas;
while (isRunning) {
if (!ourHolder.getSurface().isValid())
continue;
canvas = ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150);
float centerX = canvas.getWidth() / 2;
float centerY = canvas.getHeight() / 2;
canvas.drawBitmap(ball, centerX + sensorX * 20, centerY
+ sensorY * 20, null);
ourHolder.unlockCanvasAndPost(canvas);
Log.v("message", "run startede");
}
} catch (Exception ex) {
// TODO: handle exception
Log.e("mytag", "mymessage", ex);
}
}
}
}
记录猫
03-07 11:06:27.957: D/AndroidRuntime(749): Shutting down VM
03-07 11:06:27.967: W/dalvikvm(749): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-07 11:06:28.007: E/AndroidRuntime(749): FATAL EXCEPTION: main
03-07 11:06:28.007: E/AndroidRuntime(749): android.app.SuperNotCalledException: Activity {com.ss/com.ss.Accelerate} did not call through to super.onCreate()
03-07 11:06:28.007: E/AndroidRuntime(749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
03-07 11:06:28.007: E/AndroidRuntime(749): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-07 11:06:28.007: E/AndroidRuntime(749): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-07 11:06:28.007: E/AndroidRuntime(749): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-07 11:06:28.007: E/AndroidRuntime(749): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 11:06:28.007: E/AndroidRuntime(749): at android.os.Looper.loop(Looper.java:137)
03-07 11:06:28.007: E/AndroidRuntime(749): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-07 11:06:28.007: E/AndroidRuntime(749): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 11:06:28.007: E/AndroidRuntime(749): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 11:06:28.007: E/AndroidRuntime(749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-07 11:06:28.007: E/AndroidRuntime(749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-07 11:06:28.007: E/AndroidRuntime(749): at dalvik.system.NativeStart.main(Native Method)
03-07 11:06:31.887: I/Process(749): Sending signal. PID: 749 SIG: 9
答案 0 :(得分:2)
03-07 11:06:28.007: E/AndroidRuntime(749): android.app.SuperNotCalledException: Activity {com.ss/com.ss.Accelerate} did not call through to super.onCreate()
请先在super.onCreate(savedInstanceState)
班级的onCreate()
方法中先致电Accelerate
。