我在主活动中设置了一个带有三个标签的开关卡,在第三个标签中有一个列表视图,单击列表视图会弹出时间选项对话框。
public void onSetWarn(){
LayoutInflater factoryInflater = LayoutInflater.from(MainActivity.this);
final View dialogView = factoryInflater.inflate(R.layout.dialog, null);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("set")
.setView(dialogView)
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "set success!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
/*
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
*/
dialog.show();
}
我应该把这段代码放在哪里
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
我trid几个地方,但所有的NULL指针错误。
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reminder the time"
/>
<TimePicker
android:id="@+id/TimePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remind way"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ring"
/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="shake"
/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
您应该使用dialogView.findViewById(R.id.TimePicker1)
而不是this.findViewById(R.id.TimePicker1)
。这是因为在SessionView的视图树中存在TimePicker1(您可以通过LayoutInflator从dialog.xml加载它)