Android-无法在CustomViews上找到ViewByid()

时间:2018-10-31 05:35:47

标签: java android android-layout

我刚开始编写一个小型应用程序,以从自定义时间选择器中选择时间并从中获取时间并使用它。我得到了NullRefrenceError

我的xml文件:

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

这里TimePick是一个Java类,可以很好地用于拾取时间。

代码:

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

这是我遇到的错误:

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");

            }
        });
    }

}

获取

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.TimePicker.setOnTimeChangedListener(android.widget.TimePicker $ OnTimeChangedListener)”

我该如何解决?

2 个答案:

答案 0 :(得分:0)

尝试此代码

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

答案 1 :(得分:0)

您应该以不同的方式进行设计。您应该扩展TimePick类,一次扩展time_from,一次扩展time_two。在扩展类而不是onClickListener类中设置PreferencesScreen。您应该像这样重写onCreateDialogView函数。

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

然后在XML中,将其放在Extended类中,而不是TimePick类中。