我是 Android 应用程序开发的初学者。使用eclipse开发一个简单的蓝牙摇床应用。在我的手机上试了一下,运行了大约 30 秒。然后它关闭说“不幸的是TheApp已停止”。这个应用程序应该通过摇晃打开蓝牙并播放哔声(pop.mp3)
请帮忙。
主.java>
public class BlueActivity extends Activity implements SensorListener, OnCompletionListener {
Button a, b,d;
EditText e;
TextView f;
protected final int id=1;
BluetoothAdapter bt;
public MediaPlayer m;
private SensorManager sensorMgr;
private long lastUpdate = -1;
private float x, y, z;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue);
a=(Button)findViewById(R.id.button1);
b=(Button)findViewById(R.id.button2);
d=(Button)findViewById(R.id.button3);
f=(TextView)findViewById(R.id.textView1);
e=(EditText)findViewById(R.id.editText1);
e.setFocusable(false);
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = bt.getAddress();
e.setText(s);
}
});
a.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(i, id);
}
});
d.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("http://www.gamerspitch.com");
Intent in = new Intent(Intent.ACTION_VIEW, uri);
startActivity(in);
}
});
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
boolean accelSupported sensorMgr.registerListener(this,SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_NORMAL);
bt= BluetoothAdapter.getDefaultAdapter();
if (!accelSupported) {
// on accelerometer on this device
sensorMgr.unregisterListener(this,SensorManager.SENSOR_ACCELEROMETER);
}
}
protected void onPause() {
m = MediaPlayer.create(this, R.raw.pop);
m.setOnCompletionListener(this);
if (sensorMgr != null) {
sensorMgr.registerListener(this,SensorManager.SENSOR_ACCELEROMETER);
sensorMgr = null;
}
if(bt.isEnabled())
m.start();
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blue, menu);
return true;
}
@Override
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(int sensor, float[] values) {
// TODO Auto-generated method stub
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime - lastUpdate)> 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
m = MediaPlayer.create(this, R.raw.pop);
m.setOnCompletionListener(this);
x = values[SensorManager.DATA_X];
y = values[SensorManager.DATA_Y];
z = values[SensorManager.DATA_Z];
float speed=Math.abs(x+y+z - last_x - last_y - last_z)/ diffTime*10000;
if (speed > SHAKE_THRESHOLD) {
// yes, this is a shake action! Do something about it!
//Toast.makeText(this, "Shaking", Toast.LENGTH_SHORT).show();
if(!bt.isEnabled())
{
bt.enable();
f.setText("ON");
m.start();
}
else
{
bt.disable();
f.setText("OFF");
}
}
last_x = x;
last_y = y;
last_z = z;
}
}
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
}
它与异常处理有关吗?因为我很不擅长那个。
清单文件.xml >
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gamerspitch.easybluetooth"
android:anyDensity="false"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gamerspitch.easybluetooth.SplashScreen"
android:label="@string/title_activity_splash_screen"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.gamerspitch.easybluetooth.BlueActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
日志猫 >
`08-08 16:03:33.112: E/SensorManager(4428): Exception dispatching input event.
08-08 16:03:33.112: E/AndroidRuntime(4428): FATAL EXCEPTION: main
08-08 16:03:33.112: E/AndroidRuntime(4428): java.lang.NullPointerException
08-08 16:03:33.112: E/AndroidRuntime(4428): at com.gamerspitch.easybluetooth.BlueActivity.onSensorChanged(BlueActivity.java:163)
08-08 16:03:33.112: E/AndroidRuntime(4428): at android.hardware.LegacySensorManager$LegacyListener.onSensorChanged(LegacySensorManager.jav a:274)
08-08 16:03:33.112: E/AndroidRuntime(4428): at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:371)
08-08 16:03:33.112: E/AndroidRuntime(4428): at android.os.MessageQueue.nativePollOnce(Native Method)
08-08 16:03:33.112: E/AndroidRuntime(4428): at android.os.MessageQueue.next(MessageQueue.java:132)
08-08 16:03:33.112: E/AndroidRuntime(4428): at android.os.Looper.loop(Looper.java:124)
08-08 16:03:33.112: E/AndroidRuntime(4428): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-08 16:03:33.112: E/AndroidRuntime(4428): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 16:03:33.112: E/AndroidRuntime(4428): at java.lang.reflect.Method.invoke(Method.java:525)
08-08 16:03:33.112: E/AndroidRuntime(4428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-08 16:03:33.112: E/AndroidRuntime(4428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 16:03:33.112: E/AndroidRuntime(4428): at dalvik.system.NativeStart.main(Native Method)
`
先感谢您。