0

I'm new to coding writing a small application to select time from a custom time picker and get time from it and use it.I'm getting a NullRefrenceError.

My xml file:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:key="init_Settings">
<PreferenceCategory
    android:title="Set-Time">
    <com.lambdahash.sonic.ui_app.fragments.TimePick
        android:id="@+id/from_time"
        android:title="Time from"
        android:defaultValue="--:--"
        android:summary="--:--"
        android:key="time-from"
        />
    <com.lambdahash.sonic.ui_app.fragments.TimePick
        android:id="@+id/to_time"
        android:title="Time to"
        android:summary="--:--"
        android:defaultValue="--:--"
        android:key="time-to"
        />
</PreferenceCategory>

Here TimePick is a java class that works perfectly fine for picking time.

The code:

public class TimePick extends DialogPreference {
    private int lastHour=0;
    private int lastMinute=0;
    private TimePicker picker=null;
    public static int getHour(String time) {
        String[] pieces=time.split(":");

        return(Integer.parseInt(pieces[0]));
    }

    public static int getMinute(String time) {
        String[] pieces=time.split(":");

        return(Integer.parseInt(pieces[1]));
    }

    public TimePick(Context ctxt, AttributeSet attrs) {
        super(ctxt, attrs);

        setPositiveButtonText("Set");
        setNegativeButtonText("Cancel");
    }

    @Override
    protected View onCreateDialogView() {
        picker=new TimePicker(getContext());

        return(picker);
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);

        picker.setCurrentHour(lastHour);
        picker.setCurrentMinute(lastMinute);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult) {
            lastHour=picker.getCurrentHour();
            lastMinute=picker.getCurrentMinute();

            String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

            if (callChangeListener(time)) {
                persistString(time);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return(a.getString(index));
    }
}

This is where I'm getting error:

public class generalSettings extends PreferenceActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.general_settings);

        TimePicker time_from = (TimePicker) findViewById(R.id.from_time);
        TimePicker time_to = (TimePicker) findViewById(R.id.to_time);

      time_from.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                //ts.timeFrom();
                Log.d("TESTF:",hourOfDay+":"+minute+"\n");

            }
        });
    }

}

Getting

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TimePicker.setOnTimeChangedListener(android.widget.TimePicker$OnTimeChangedListener)' on a null object reference

How do I workaround this?

Milad Bahmanabadi
  • 946
  • 11
  • 27
Sonic_
  • 347
  • 3
  • 9
  • Post your addPreferencesFromResource's code please. – aolphn Oct 31 '18 at 05:41
  • That's not a function I wrote it's a built in android function used in PreferenceActivity. – Sonic_ Oct 31 '18 at 05:45
  • I think you didn't setContentView for your activity. – aolphn Oct 31 '18 at 05:46
  • setContentView() is used for Activity and addPreferencesFromResource() is used for PreferenceActivity, addPreferencesFromResource() adds from an "xml" folder and setContentView() adds from "layout" folder. – Sonic_ Oct 31 '18 at 05:50
  • maybe you can check this question,it's similar with your issue.https://stackoverflow.com/questions/15728192/usage-of-findviewbyid-in-preferenceactivity – aolphn Oct 31 '18 at 05:56
  • He's not able to access any id's, my problem is just that I do not know how to access TimePick from the xml file, using findViewById(). – Sonic_ Oct 31 '18 at 06:09

2 Answers2

0

Try this code

Preference time_from = findPreference("time-from");

time_from.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        //ts.timeFrom();
        Log.d("TESTF:",hourOfDay+":"+minute+"\n");
    }
});


Preference time_to= findPreference("time-to");

time_from.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
   @Override
   public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
       //ts.timeTo();
       Log.d("TESTF:",hourOfDay+":"+minute+"\n");
   }
});
ShehrozEK
  • 180
  • 1
  • 1
  • 6
  • Well `findPreference("time-from");` finds prefrances and `.setOnTimeChangedListener` can be applied to only TimePick as it extends DialogPreference. – Sonic_ Oct 31 '18 at 08:24
0

You should be designing it differently. You should be extending the TimePick class, once for the time_from and once for time_two. Set the onClickListener in the extended class, not in the PreferencesScreen class. You should do that by overriding the onCreateDialogView function, like this.

@Override protected View onCreateDialogView() { 
    picker=super.onCreateDialogView();
    picker.setOnTimeChangedListener(...);
    return(picker); 
} 

Then in your XML, put in your Extended class, rather than the TimePick class.

lionscribe
  • 3,413
  • 1
  • 16
  • 21