0

我在主activity中设置了一个带有三个选项卡的交换卡,第三个选项卡中有一个listview,点击listview会弹出时间选项对话框。

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

我尝试了几个地方,但所有的 NULL 指针错误。

对话框.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>
4

1 回答 1

2

您应该使用dialogView.findViewById(R.id.TimePicker1)而不是this.findViewById(R.id.TimePicker1)。那是因为 TimePicker1 存在于 dialogView 的视图树中(您通过 LayoutInflator 从 dialog.xml 加载它)

于 2012-09-03T02:21:47.220 回答