解决方案 »

  1.   

    以下是错误提示
    03-09 11:03:57.831: D/dalvikvm(1266): GC_CONCURRENT freed 62K, 7% free 2764K/2968K, paused 21ms+4ms, total 83ms
    03-09 11:03:57.831: D/dalvikvm(1266): WAIT_FOR_CONCURRENT_GC blocked 17ms
    03-09 11:03:57.861: I/dalvikvm-heap(1266): Grow heap (frag case) to 5.835MB for 3145744-byte allocation
    03-09 11:03:57.981: D/dalvikvm(1266): GC_FOR_ALLOC freed 2K, 4% free 5833K/6044K, paused 113ms, total 114ms
    03-09 11:03:58.051: D/dalvikvm(1266): GC_CONCURRENT freed <1K, 4% free 5833K/6044K, paused 9ms+17ms, total 76ms
    03-09 11:03:58.571: I/Choreographer(1266): Skipped 97 frames!  The application may be doing too much work on its main thread.
    03-09 11:03:58.591: D/gralloc_goldfish(1266): Emulator without GPU emulation detected.
    03-09 11:04:04.741: E/BluetoothAdapter(1266): Bluetooth binder is null
    03-09 11:04:04.741: D/AndroidRuntime(1266): Shutting down VM
    03-09 11:04:04.761: W/dalvikvm(1266): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    03-09 11:04:04.861: E/AndroidRuntime(1266): FATAL EXCEPTION: main
    03-09 11:04:04.861: E/AndroidRuntime(1266): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.energysavingofmobilephone/com.example.energysavingofmobilephone.EnergySaving}: java.lang.NullPointerException
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.os.Handler.dispatchMessage(Handler.java:99)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.os.Looper.loop(Looper.java:137)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.app.ActivityThread.main(ActivityThread.java:5041)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at java.lang.reflect.Method.invoke(Method.java:511)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at dalvik.system.NativeStart.main(Native Method)
    03-09 11:04:04.861: E/AndroidRuntime(1266): Caused by: java.lang.NullPointerException
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at com.example.energysavingofmobilephone.EnergySaving.onCreate(EnergySaving.java:132)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.app.Activity.performCreate(Activity.java:5104)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    03-09 11:04:04.861: E/AndroidRuntime(1266):  ... 11 more
    03-09 11:04:07.001: I/Process(1266): Sending signal. PID: 1266 SIG: 9
      

  2.   

    manifest文件中声明这个activity了么
      

  3.   

    无法启动该activity,空指针的错误,明显不是这段代码的原因么
      

  4.   

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.content.BroadcastReceiver;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.Uri;
    import android.net.wifi.WifiManager;
    import android.os.BatteryManager;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.provider.Settings.SettingNotFoundException;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.ImageView;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    import android.widget.Switch;
    import android.widget.TextView;
    import android.widget.Toast;@SuppressLint("NewApi")
    public class EnergySaving extends Activity { private int level;
    private int status;
    ImageView imageView1;
    TextView textView1;
    TextView textView3;
    CheckBox checkBox1;
    private SeekBar seekBar;
    Switch switch1;
    Switch switch2;
    WifiManager wifiManager;
    BluetoothAdapter mBluetoothAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.energysaving);
    this.setTitle("节能控制");
    registerReceiver(batteryChangedReceiver, new IntentFilter(
    Intent.ACTION_BATTERY_CHANGED));
    imageView1 = (ImageView) findViewById(R.id.imageView1);
    textView1 = (TextView) findViewById(R.id.textView1);
    textView3 = (TextView) findViewById(R.id.textView3); textView1.setTextSize(22); seekBar = (SeekBar) findViewById(R.id.seekBar1);
    seekBar.setMax(255);// 最大亮度为255,设定进度条的max为255
    int nowBrightnessValue = 0;
    ContentResolver resolver = this.getContentResolver();
    try {
    nowBrightnessValue = android.provider.Settings.System.getInt(
    resolver, Settings.System.SCREEN_BRIGHTNESS);
    } catch (SettingNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    textView3.setText("当前屏幕亮度:" + nowBrightnessValue);
    seekBar.setProgress(nowBrightnessValue);// 先显示,后看是否手动还是自动调节亮度
    checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    checkBox1.setOnClickListener(new View.OnClickListener() { @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if (checkBox1.isChecked()) {
    Settings.System.putInt(getContentResolver(),
    Settings.System.SCREEN_BRIGHTNESS_MODE,
    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);// 关闭自动调节
    seekBar.setEnabled(true);
    } else { Settings.System.putInt(getContentResolver(),
    Settings.System.SCREEN_BRIGHTNESS_MODE,
    Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);// 未选中手动调节,则改为自动调节亮度
    seekBar.setEnabled(false); }
    }
    });
    seekBar.setOnSeekBarChangeListener(new SeekBarChangeListenerImp());
    switch1 = (Switch) findViewById(R.id.switch1);
    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
    switch1.setChecked(true);
    } else {
    switch1.setChecked(false); }
    switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
    public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
    // TODO Auto-generated method stub
    if (isChecked) {
    wifiManager.setWifiEnabled(true); Toast.makeText(EnergySaving.this,
    "当前Wifi网卡状态为1" + wifiManager.getWifiState(),
    Toast.LENGTH_SHORT).show(); } else {
    wifiManager.setWifiEnabled(false); Toast.makeText(EnergySaving.this,
    "当前Wifi网卡状态为2" + wifiManager.getWifiState(),
    Toast.LENGTH_SHORT).show();
    }
    }
    });
    switch2=(Switch)findViewById(R.id.switch2);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter.getState()==BluetoothAdapter.STATE_ON){
    switch2.setChecked(true);
    } else {
    switch2.setChecked(false); }
    switch2.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
    public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
    // TODO Auto-generated method stub
    if (isChecked) {
    mBluetoothAdapter.enable(); Toast.makeText(EnergySaving.this,
    "当前蓝牙状态为1" + mBluetoothAdapter.getState(),
    Toast.LENGTH_SHORT).show(); } else {
    mBluetoothAdapter.disable(); Toast.makeText(EnergySaving.this,
    "当前蓝牙状态为2" + mBluetoothAdapter.getState(),
    Toast.LENGTH_SHORT).show();
    }
    }
    }); } class SeekBarChangeListenerImp implements OnSeekBarChangeListener { public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
    // TODO Auto-generated method stub setScreenBrightness(seekBar.getProgress());// 传入当前亮度 } public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    } public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    }
    } // 设置屏幕亮度的函数 public void setScreenBrightness(int num) {
    // TODO Auto-generated method stub
    ContentResolver resolver = this.getContentResolver(); WindowManager.LayoutParams layoutParams = super.getWindow()
    .getAttributes();
    layoutParams.screenBrightness = (float) num / 255;// 设置屏幕的亮度
    // 要0~1,所以除以255
    super.getWindow().setAttributes(layoutParams);
    layoutParams = getWindow().getAttributes();
    textView3.setText("当前屏幕亮度:"
    + String.valueOf(layoutParams.screenBrightness * 255)); // 显示0~255而不是0~1
    // 以下 几行保存亮度,即使退出app也是当前亮度
    Uri uri = android.provider.Settings.System
    .getUriFor("screen_brightness"); android.provider.Settings.System.putInt(resolver, "screen_brightness",
    num); resolver.notifyChange(uri, null);
    } private BroadcastReceiver batteryChangedReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
    level = intent.getIntExtra("level", 0);
    int scale = intent.getIntExtra("scale", 100);
    status = intent.getIntExtra("status", 0); setBatteryInfo();
    }
    }
    }; protected void setBatteryInfo() {
    // TODO Auto-generated method stub
    // 动态改变电量图片
    int resID = getResources().getIdentifier("stat_sys_battery_" + level,
    "drawable", "com.example.energysavingofmobilephone");
    imageView1.setImageResource(resID);
    if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
    textView1.setTextColor(android.graphics.Color.BLUE);
    textView1.setText("充电中");
    } else {
    textView1.setTextColor(android.graphics.Color.GREEN);
    textView1.setText("未连接电源"); } }}
      

  5.   

    空指针是应为虚拟机没有蓝牙,无法获取到BluetoothAdapter.getDefaultAdapter();到真机上测试正常。