当我尝试通过单击按钮启动新活动时,应用程序无法停止。
在我没有使用Accleration传感器之前做到这一点并且它工作得非常好但是在添加之后它出现了在尝试启动新活动后停止的问题。
我的MainActivity看起来像这样:
package com.example.accelerationint;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements SensorEventListener, OnClickListener {
TextView accel;
Sensor accelerometer;
SensorManager sm1;
Button btnSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm1 = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sm1.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm1.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
accel = (TextView) findViewById(R.id.textAccCurrent);
btnSet = (Button) findViewById(R.id.buttonSet);
btnSet.setOnClickListener(this);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
accel.setText("X:"+event.values[0]+"\nY:"+event.values[1]+"\nZ: "+event.values[2]);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(this , SettingsActivity.class);
startActivity(i);
finish();
}
}
我的第二项活动:
package com.example.accelerationint;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SettingsActivity extends ActionBarActivity implements OnClickListener {
Button btnApply;
EditText editALimit , editDeltaT , editTWait , editALow , editAIntermediate , editAHigh ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
editALimit = (EditText) findViewById(R.id.editALimit);
editDeltaT = (EditText) findViewById(R.id.editDeltaT);
editTWait = (EditText) findViewById(R.id.editTWait);
editALow = (EditText) findViewById(R.id.editALow);
editAIntermediate = (EditText) findViewById(R.id.editAIntermediate);
editAHigh = (EditText) findViewById(R.id.editAHigh);
btnApply = (Button) findViewById(R.id.buttonSet);
btnApply.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
和Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.accelerationint"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="13" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
</application>
</manifest>
LogCat错误:
07-17 23:18:36.324: E/AndroidRuntime(5423): FATAL EXCEPTION: main
07-17 23:18:36.324: E/AndroidRuntime(5423): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accelerationint/com.example.accelerationint.SettingsActivity}: java.lang.NullPointerException
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.app.ActivityThread.access$700(ActivityThread.java:134)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.os.Looper.loop(Looper.java:137)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.app.ActivityThread.main(ActivityThread.java:4867)
07-17 23:18:36.324: E/AndroidRuntime(5423): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 23:18:36.324: E/AndroidRuntime(5423): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 23:18:36.324: E/AndroidRuntime(5423): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-17 23:18:36.324: E/AndroidRuntime(5423): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-17 23:18:36.324: E/AndroidRuntime(5423): at dalvik.system.NativeStart.main(Native Method)
07-17 23:18:36.324: E/AndroidRuntime(5423): Caused by: java.lang.NullPointerException
07-17 23:18:36.324: E/AndroidRuntime(5423): at com.example.accelerationint.SettingsActivity.onCreate(SettingsActivity.java:33)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.app.Activity.performCreate(Activity.java:5047)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-17 23:18:36.324: E/AndroidRuntime(5423): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
07-17 23:18:36.324: E/AndroidRuntime(5423): ... 11 more
答案 0 :(得分:1)
您可能需要取消注册传感器侦听器onDestroy()
。
编辑:现在我查看了堆栈跟踪,我怀疑您的设置布局中没有ID为R.id.buttonSet
的视图。崩溃发生在SettingsActivity.onCreate()
中,唯一依赖于有效引用的方法是您设置单击侦听器的位置。