1

I am trying to read stuff from a SeekBar in Android that's within a DialogFragment. It keep crashing. The SeekBar alone comes up no problem, but whenever I try to override functions so I can use the SeekBar for something it keeps crashing. I've tried copying basically what I've seen other people post as working code (though not within a DialogFragment) but it doesn't work. Can someone tell me what's wrong here?

    numColumnsControl = (SeekBar) getActivity().findViewById(R.id.seekBar1);
    numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    {
        //@Override
        public void onProgressChanged(SeekBar arg0, int progress, boolean fromTouch) 
        {
            ((TextView) (getActivity().findViewById(R.id.textView2))).setText(String.valueOf(progress));

        }

        //@Override
        public void onStartTrackingTouch(SeekBar arg0) 
        {
            // TODO Auto-generated method stub

        }

        //@Override
        public void onStopTrackingTouch(SeekBar arg0) 
        {
            // TODO Auto-generated method stub

        }
    });`

It keeps crashing whenever I perform the action to open the DialogFragment. When I comment out the entire section it is fine. The problem is somehow overriding those functions, even when they're empty. When this entire section is commented the View comes up with SeekBars and TextViews.

I don't think this is important but here's the XML for the DialogFragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/titleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Settings"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/seekBar1"
        android:layout_marginTop="20dp"
        android:text="NumShifts"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2" 
        android:gravity="center"
        android:layout_gravity="center"
        android:max="15"
        android:progress="6"
        android:secondaryProgress="0"/>

    <SeekBar
        android:id="@+id/SeekBar01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3" 
        android:gravity="center"
        android:layout_gravity="center"
        android:max="9"
        android:progress="3"
        android:secondaryProgress="0"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/titleText"
        android:layout_below="@+id/titleText"
        android:layout_marginTop="18dp"
        android:text="NumColumns"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

</LinearLayout>

Here is the whole code from the class:

package com.example.[I have to hide this name];

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SettingsDialogueFragment extends DialogFragment
{
private int numColums;
private int numShifts;
private Context ctx;
private SeekBar numColumnsControl = null;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) 
{
    ctx = getActivity();
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater= getActivity().getLayoutInflater();


    builder.setView(inflater.inflate(R.layout.settings_menu, null))
        .setPositiveButton("DONE", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                //done
            }
        })
        .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                // User cancelled the dialog
            }
        });
    // Create the AlertDialog object and return it
    LinearLayout settings = new LinearLayout(ctx);

    numColumnsControl = (SeekBar) getActivity().findViewById(R.id.seekBar1);
    Activity myActivity = getActivity();
    if(myActivity == null)
    {
        Log.e("bill", "bill");
    }
    if(numColumnsControl == null)
    {
        Log.e("smith", "smith");
    }
    numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    {
        //@Override
        public void onProgressChanged(SeekBar arg0, int progress, boolean fromTouch) 
        {
            ((TextView) (getActivity().findViewById(R.id.textView2))).setText(String.valueOf(progress));
            Log.e("asd", "asd");

        }

        //@Override
        public void onStartTrackingTouch(SeekBar arg0) 
        {
            Log.e("asdf", "asdf");
            // TODO Auto-generated method stub

        }

        //@Override
        public void onStopTrackingTouch(SeekBar arg0) 
        {
            Log.e("asdfg", "asdfg");
            // TODO Auto-generated method stub

        }
    });

    return builder.create();
}
}

1 Answers1

0

Based on your comment

'The error is a NullPointerException, which occurs on line 51 which is the 2nd line in the fragment I gave you ("numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() ")'

Check if findViewById(R.id.Seekbar1) doesn't returns null. If it does, Make sure your dialog layout is inflated before requesting any view elements from the layout. It is also possible something is going wrong with the R references (try project -> clean in eclipse) .

Edit I'm not sure if it works, but try to overide onViewCreated() and place your findViewById and setOnSeekBarChangeListener code here.

Edit in some way onViewCreated() is not called (Android DialogFragment onViewCreated not called). This makes the solution a little more difficult. You need to inflate the view first and perform an findViewById on the inflated view.

View menuView = inflater.inflate(R.layout.settings_menu);
numColumnsControl = (SeekBar) menuView.findViewById(R.id.seekBar1, null);
numColumnsControl.setOnSeekBarChangeListener ...
builder.setView(menuView);
Community
  • 1
  • 1
Bas
  • 881
  • 1
  • 10
  • 23
  • The problem is the 2nd one Bas said. I have inflated the layout though, and cleaning the project didn't do anything. I'll edit the top post to include all the code. – user2471612 Jun 20 '13 at 14:38
  • I've included a possible solution. Can you check if it works? – Bas Jun 20 '13 at 14:52
  • 1
    Hey Bas, it no longer crashes when I do that but onViewCreated doesn't ever seem to occur. The DialogFragment comes up but I put a log in onViewCreated and it never occurs. Do you know why that would happen? – user2471612 Jun 20 '13 at 15:04
  • In some way it is not called in a dialogfragment... I've included a new solution that probably works. – Bas Jun 20 '13 at 15:31
  • Hey Bes, I think that works. It pulls up the dialogFragment but it crashes when I try to scroll the SeekBars. I'm gonna do some debugging, it's likely an unrelated issue. Thanks a lot for the help man! – user2471612 Jun 20 '13 at 15:49
  • findViewById(R.id.Textview2) returns null? ;) Same problem, use menuView.findViewById(...). Glad I could help. – Bas Jun 20 '13 at 16:02