1

-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;
}

}

a.kollar
  • 43
  • 6

1 Answers1

3

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.

Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
  • I have a UI and even when I run it with the UI it still crashes out. – a.kollar Jul 06 '12 at 18:24
  • What do you mean you have a UI? The way the activity attaches itself to a UI is through setContentView() as described above. – CSAntol Jul 06 '12 at 18:27
  • I mean I have used it in the code before, but even with the Layout call it still crashes. – a.kollar Jul 06 '12 at 18:27
  • @VenomVendor http://pastebin.stonekeep.com/12188 <---xml for the UI If you want the Manifest I can get that easy enough – a.kollar Jul 06 '12 at 18:44
  • Am Sure that You are missing this "setContentView(R.layout.your_layout);" as mentioned in the above answer. If it still crashes then Get me the Manifest too – VenomVendor Jul 06 '12 at 19:10
  • @a.kollar Can you post the complete code of the Activity? I'm wondering how you are using `findPreference()` in a regular activity. Usually you'd have to use a PreferenceActivity. How is your code compiling? – Benito Bertoli Jul 06 '12 at 19:15
  • @Benito Actually that might be my issue is that It cant be run as a normal activity. But sure I'll post the whole thing. Give me a minute. – a.kollar Jul 06 '12 at 19:22
  • @Benito There its up in OP hope that helps a little. – a.kollar Jul 06 '12 at 19:26
  • I had figured it out before you posted the code. Check out my edited answer. – Benito Bertoli Jul 06 '12 at 19:28
  • @Benito Thank you! Now there are a few more errors (in code) but its progress! – a.kollar Jul 06 '12 at 19:32
  • Yes, but now it's another error: *Your content must have a ListView whose id attribute is 'android.R.id.list'*. That's because PreferenceActivity extends ListActivity. Read [this](http://myandroidsolutions.blogspot.com/2012/03/android-preferenceactivity.html) tutorial because this is getting out of scope. – Benito Bertoli Jul 06 '12 at 19:48