0

我现在大部分时间都在玩这个偏好活动代码,在我上一篇文章中的一些帮助下,我得到了它,所以它可以在 AVD 上编译/安装,但它不会运行并且不会显示在应用程序菜单中。这是代码:

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
public class volman extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dhd);

    DialogPreference dp = (DialogPreference) findPreference("mediavolume");
    dp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference,
                Object newValue) {
            SeekBar volumeBar = (SeekBar) findViewById(R.id.seekBar);
            final AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

            volumeBar.setMax(manager
                    .getStreamMaxVolume(AudioManager.STREAM_SYSTEM));
            volumeBar.setProgress(manager
                    .getStreamVolume(AudioManager.STREAM_SYSTEM));

            volumeBar
                    .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                            Toast.makeText(volman.this, "Starting Vol Tracking", Toast.LENGTH_LONG).show();

                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                            Toast.makeText(volman.this, "Now Stopping", Toast.LENGTH_LONG).show();

                        }
                        public void onProgressChanged(SeekBar seekBar,
                                int progress, boolean fromUser) {
                            manager.setStreamVolume(
                                    AudioManager.STREAM_SYSTEM, progress,
                                    AudioManager.FLAG_SHOW_UI);
                            Toast.makeText(volman.this, "Now going Silent!", Toast.LENGTH_LONG).show();
                        }
                    });

            return false;
        }
    });
}

private DialogPreference findPreference(String string) {
    return null;
}
}  

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="path.to.app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="5"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".volman"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.PREFERENCE" />
        </intent-filter>
    </activity>
</application>

4

1 回答 1

0

你从

DialogPreference dp = (DialogPreference) findPreference("mediavolume");

但是之后

private DialogPreference findPreference(String string) {
    return null;
}

所以你得到了一个NullPointerException例外dp.setOnPreferenceChangeListener(...),不是吗?

于 2012-07-06T20:38:25.363 回答