麻烦膨胀第二个布局

时间:2013-11-10 02:57:52

标签: android runtime inflate

我正在尝试在我的活动中设置第二个膨胀的布局,一个工作膨胀以从时间戳设置警报,第二个膨胀数字选择器以设置金额。到目前为止,第二个按钮不会响应,或者根据另一组代码(显示一个),我得到一个运行时错误。谁能告诉我我做错了什么?

package org.iimed.www;

import java.util.Calendar;

import org.iimed.www.DateTimePicker.DateWatcher;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class CaAdd extends Activity implements OnClickListener {
    Button button_click, amount;
    TextView dateResult, date2;
    int hrs, min;
    int amountt = 0;
    EditText amountedit;
    String amountText;
    Dialog amountdialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datetimeactivity);
        dateResult = (TextView) findViewById(R.id.textview1);
        date2 = (TextView) findViewById(R.id.textView2);
        button_click = (Button) findViewById(R.id.setbtn);
        amount = (Button) findViewById(R.id.amountbtn);
        amount.setOnClickListener(this);
        button_click.setOnClickListener(this);

    }

    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.setbtn:

            amountdialog = new Dialog(this);

            final Dialog mDateTimeDialog = new Dialog(this);
            // Inflate the root layout
            final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater()
                    .inflate(R.layout.date_time_dialog, null);
            // Grab widget instance
            final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView
                    .findViewById(R.id.DateTimePicker);

            mDateTimePicker.initData();

            Button setDateBtn = (Button) mDateTimeDialogView
                    .findViewById(R.id.SetDateTime);
            // Update demo TextViews when the "OK" button is clicked
            setDateBtn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(CaAdd.this, AlarmReceiver.class);
                    PendingIntent pi = PendingIntent.getActivity(CaAdd.this, 2,
                            intent, PendingIntent.FLAG_CANCEL_CURRENT);
                    AlarmManager alm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

                    hrs = mDateTimePicker.getHour();
                    min = mDateTimePicker.getMinute();
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, hrs);
                    calendar.set(Calendar.MINUTE, min);
                    calendar.set(Calendar.SECOND, 0);
                    alm.set(AlarmManager.RTC_WAKEUP,
                            calendar.getTimeInMillis(), pi);

                    mDateTimePicker.clearFocus();
                    String result_string = mDateTimePicker.getMonth() + "/"
                            + String.valueOf(mDateTimePicker.getDay()) + "/"
                            + String.valueOf(mDateTimePicker.getYear()) + "  "
                            + String.valueOf(mDateTimePicker.getHour()) + ":"
                            + String.valueOf(mDateTimePicker.getMinute());

                    if (mDateTimePicker.getHour() > 12)
                        result_string = result_string + "PM";
                    else
                        result_string = result_string + "AM";
                    date2.setText(result_string);

                    mDateTimeDialog.dismiss();
                }
            });

            Button cancelBtn = (Button) mDateTimeDialogView
                    .findViewById(R.id.CancelDialog);
            // Cancel the dialog when the "Cancel" button is clicked
            cancelBtn.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    mDateTimePicker.reset();
                    mDateTimeDialog.cancel();
                }
            });

            // Reset Date and Time pickers when the "Reset" button is clicked

            Button resetBtn = (Button) mDateTimeDialogView
                    .findViewById(R.id.ResetDateTime);
            resetBtn.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    mDateTimePicker.reset();
                }
            });

            Button amountbtn = (Button) findViewById(R.id.amountbtn);
            amountbtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    // Inflate the root layout
                    final RelativeLayout amountView = (RelativeLayout) getLayoutInflater()
                            .inflate(R.layout.amount, null);
                    // Grab widget instance

                    amountdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                    amountdialog.setContentView(amountView);

                    amountdialog.show();
                    amountedit = (EditText) amountView
                            .findViewById(R.id.amount);
                    ImageButton increase = (ImageButton) amountView
                            .findViewById(R.id.increase);
                    increase.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {

                            amountt++;
                            amountText = Integer.toString(amountt);
                            amountedit.setHint(amountText);
                        }
                    });

                    ImageButton decrease = (ImageButton) amountView
                            .findViewById(R.id.decrease);
                    decrease.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            amountt--;
                            amountText = Integer.toString(amountt);
                            amountedit.setHint(amountText);

                        }
                    });
                    amountdialog.dismiss();
                    // No title on the dialog window
                    mDateTimeDialog
                            .requestWindowFeature(Window.FEATURE_NO_TITLE);
                    // Set the dialog content view
                    mDateTimeDialog.setContentView(mDateTimeDialogView);
                    // Display the dialog
                    mDateTimeDialog.show();

                }
            });
        }
    }
}

0 个答案:

没有答案