public void pickTimeHandler(View target) {
Toast.makeText(Alarms1.this, "in PickTimehandler", Toast.LENGTH_LONG).show();
showDialog(0);
你看到Alarm1.t这是我所有代码中的错误 在范围
中无法访问Alarms1类型的封闭实例答案 0 :(得分:1)
如果这是封闭类的名称
,你会写Alarms1.this这个班的名字是什么?使用该名称。
示例:
public class Alarms1 {
public void pickTimeHandler(View target) {
//code below would work, you could also just write "this" instead of "Alarms1.this"
Toast.makeText(Alarms1.this, "in PickTimehandler", Toast.LENGTH_LONG).show();
showDialog(0);
}
}
如果它是由内部类创建的,您只需要使用类的名称限定 this 值。
以下是您需要确定使用“this”参数的示例:
public class Alarms1 {
public void onCreate(Bundle state) {
Button button = (Button) findViewById(R.id.set_alarm);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(Alarms1.this, "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
不同之处在于,在第二个示例中的上述代码中,当您放置
时,您将创建一个匿名内部类button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
如果您编写this
,那么从该代码中,java编译器/运行时会认为您指的是内部类的实例(而不是Alarms1)。
---更新---
鉴于你发给我的代码,我想出了这个:
package com.example;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void pickTimeHandler(View target) {
Toast.makeText(this, "in PickTimehandler",
Toast.LENGTH_LONG).show();
showDialog(0);
}
@Override
protected Dialog onCreateDialog(int id) {
Toast.makeText(this,
"- onCreateDialog(ID_TIMEPICKER) -",
Toast.LENGTH_LONG).show();
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
OnTimeSetListener theTimeSetListener = null;
return new TimePickerDialog(this,
theTimeSetListener,
hour, minute, false);
}
private TimePickerDialog.OnTimeSetListener theTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
String time = "User picked Hour: " + String.valueOf(hour) + "\n"
+ "Minute: " + String.valueOf(minute);
Toast.makeText(AlarmActivity.this, time, Toast.LENGTH_LONG).show();
int theHour = hour;
int theMinute = minute;
Intent alarmIntent = new Intent();
alarmIntent.putExtra("nel.example.alarms1", "My message");
Object pendingAlarmIntent = PendingIntent.getService(AlarmActivity.this, 0,
alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar AlarmCal = Calendar.getInstance();
AlarmCal.setTimeInMillis(System.currentTimeMillis());
AlarmCal.set(Calendar.HOUR_OF_DAY, theHour); // set user selection
AlarmCal.set(Calendar.MINUTE, theMinute); // set user selection
AlarmCal.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
AlarmCal.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES,
(PendingIntent) pendingAlarmIntent);
}
};
}
我所做的改变是:
PendingIntent.getService需要一个上下文。这完全是我在原始解决方案中描述的问题。 Activity是Context的子类,这意味着您可以在第一个参数中将actvity传递给PendingIntent.getService。但是,当您创建该内部类时:
private TimePickerDialog.OnTimeSetListener theTimeSetListener = new TimePickerDialog.OnTimeSetListener(){
其中的代码不再是Activity,而是TimePickerDialog.OnTimeSetListener。您需要将myMenu.this作为第一个参数传递(或者在我的情况下,因为我将您的类重命名为AlarmActivity,您将传递AlarmActivity.this)。您在内部类中创建的Toasts也是如此。
我强烈建议你停止你正在做的事情,然后继续学习java,然后继续开发android。读一本书,按照在线教程等等。但如果你只是在不了解java的情况下深入了解android,你就会遇到很多问题。