我正在开发一个打开和关闭闪光灯的应用程序,我想根据光传感器检测到的照度添加自动切换,但我无法运行我的应用程序。有很多例外,我不知道为什么。
有我的java代码:
public class MainActivity extends Activity implements SensorEventListener, OnItemSelectedListener
{
private boolean isLighOn = false;
private Camera camera;
private SensorManager mSensorManager;
private Sensor mLight;
private boolean isSwitchingSet =false;
private Parameters p;
private float bound = 30;
private Spinner spinner;
private String[] illuminance = {"On when can't see nothing", "On when dark", "On when middle dark", "Always On"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)this.getSystemService(Context.SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
spinner = (Spinner) findViewById(R.id.spinner);
final ImageButton ib = (ImageButton)findViewById(R.id.imageButton);
final Button button= (Button)findViewById(R.id.button);
this.getWindow().getDecorView().setBackgroundColor(Color.BLACK);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(mLight == null)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("We're sorry, but on Your device accelerometer isn't available")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
if(isSwitchingSet)
{
isSwitchingSet = false;
Toast.makeText(MainActivity.this, "Switching off", Toast.LENGTH_SHORT).show();
mSensorManager.unregisterListener(MainActivity.this);
}
else
{
isSwitchingSet = true;
mSensorManager.registerListener(MainActivity.this, mLight, SensorManager.SENSOR_DELAY_FASTEST);
Toast.makeText(MainActivity.this, "Switching on", Toast.LENGTH_SHORT).show();
}
}
});
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, illuminance);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
camera = Camera.open();
p = camera.getParameters();
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (isLighOn)
{
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
ib.setImageResource(R.drawable.button_power_off);
} else
{
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
ib.setImageResource(R.drawable.button_power_on_small);
}
}
});
}
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
mSensorManager.unregisterListener(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 void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType() == Sensor.TYPE_LIGHT)
{
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3)
{
switch(position)
{
case 0:
bound =30;
break;
case 1:
bound =80;
break;
case 2:
bound =170;
break;
case 3:
bound =10000;
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
我在以下文件中声明了这些权限Manifest.XML
:
这是我的例外列表:
06-29 12:02:31.106: E/AndroidRuntime(2483): FATAL EXCEPTION: main
06-29 12:02:31.106: E/AndroidRuntime(2483): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adrspo95.torch/com.adrspo95.torch.MainActivity}: java.lang.NullPointerException
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.os.Looper.loop(Looper.java:137)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-29 12:02:31.106: E/AndroidRuntime(2483): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 12:02:31.106: E/AndroidRuntime(2483): at java.lang.reflect.Method.invoke(Method.java:511)
06-29 12:02:31.106: E/AndroidRuntime(2483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-29 12:02:31.106: E/AndroidRuntime(2483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-29 12:02:31.106: E/AndroidRuntime(2483): at dalvik.system.NativeStart.main(Native Method)
06-29 12:02:31.106: E/AndroidRuntime(2483): Caused by: java.lang.NullPointerException
06-29 12:02:31.106: E/AndroidRuntime(2483): at com.adrspo95.torch.MainActivity.onCreate(MainActivity.java:94)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.Activity.performCreate(Activity.java:5104)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-29 12:02:31.106: E/AndroidRuntime(2483): ... 11 more
06-29 12:02:31.096: D/AndroidRuntime(2483): Shutting down VM
06-29 12:02:31.096: W/dalvikvm(2483): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-29 12:02:31.106: E/AndroidRuntime(2483): FATAL EXCEPTION: main
06-29 12:02:31.106: E/AndroidRuntime(2483): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adrspo95.torch/com.adrspo95.torch.MainActivity}: java.lang.NullPointerException
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.os.Looper.loop(Looper.java:137)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-29 12:02:31.106: E/AndroidRuntime(2483): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 12:02:31.106: E/AndroidRuntime(2483): at java.lang.reflect.Method.invoke(Method.java:511)
06-29 12:02:31.106: E/AndroidRuntime(2483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-29 12:02:31.106: E/AndroidRuntime(2483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-29 12:02:31.106: E/AndroidRuntime(2483): at dalvik.system.NativeStart.main(Native Method)
06-29 12:02:31.106: E/AndroidRuntime(2483): Caused by: java.lang.NullPointerException
06-29 12:02:31.106: E/AndroidRuntime(2483): at com.adrspo95.torch.MainActivity.onCreate(MainActivity.java:94)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.Activity.performCreate(Activity.java:5104)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-29 12:02:31.106: E/AndroidRuntime(2483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-29 12:02:31.106: E/AndroidRuntime(2483): ... 11 more