Question

-EDITED THE MAIN CODE IN THIS BLOCK-
This code I am writing is having an issue at start up. It starts the app GUI and says "We are sorry "Appname" has unfortunately stopped working."

[Here is the logcat Errors][1]
I'm guessing it has something to do with the start up code here it is:

@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", 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;
}

}

Was it helpful?

Solution

You have to set content view.

After super.onCreate(savedInstanceState); add:

setContentView(R.layout.your_layout);

Edit:

Here's what happened. You pasted this code into your Activity.

Eclipse gave you an error that : "The method findPreference(String) is undefined for the type volman". Which is normal because you have to extend PreferenceActivity rather than Activity.

You did the quick fix: Create method findPreference(String).

This created a method that returns null, but made your code compile.

That's why you are getting a NullPointerException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top