Tushar Monirul
Tushar Monirul

Reputation: 5064

How to use setOnSeekBarChangeListener(this); in android?

I have downloaded the latest android SDK. I am trying to use a seekbar to change a textView's text size. But the problem is when I am writing these lines

bar = (SeekBar) findViewById(R.id.seekBar1);
bar.setOnSeekBarChangeListener(this);

there is an error occurring. If I comment this line //bar.setOnSeekBarChangeListener(this);than the is running well but text size is not changing. I have tried too many ways but the app is not getting rid from that error. http://pastebin.com/2HTAXyvY this the link of my java file and http://pastebin.com/UcT3RBXL this is for error log.

Please help me to understand the error. Why it is happening?

Upvotes: 0

Views: 524

Answers (2)

Tushar Monirul
Tushar Monirul

Reputation: 5064

You use the wrong layout as fragment_main instead of activity_main.xml. As you can see here in your Activity:

setContentView(R.layout.activity_main);

You are using the activity_main layout. Whereas here in your fragment:

inflater.inflate(R.layout.fragment_main, container, false);

You use the fragment_main.xml

The simplest solution is to copy/paste all your fragment_main.xml elements inside the activity_main.xml. And remove these lines:

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment())
            .commit();
}

which call your Fragment and display it inside your FrameLayout (in activity_main.xml)

Copied from Fllo

Upvotes: 0

John
John

Reputation: 3797

Your real problem is your bar is null. Check your XML file to make sure:

  1. You are loading the correct XML file that contains your bar
  2. You are using the correct ID to find bar
  3. Make sure your IDs are spelled correctly

Upvotes: 1

Related Questions