[英]Java (Android Studio) - Cant use LocalBroadcastManager.getInstance(this)
Fairly new to both Java and Android Studio, most of my code is from tutorials and as such I may not fully understand it, although I am trying to make sure I have some level of understanding of what I am coding. Java和Android Studio都相当新,我的大部分代码都来自教程,因此我可能无法完全理解它,尽管我正在努力确保我对我编码的内容有一定程度的了解。
So I am trying to use the a DatePicker Fragment in order to pick a date and then set a text box to the chosen date. 因此,我尝试使用DatePicker片段来选择日期,然后将文本框设置为所选日期。 In order to send the string of chosen date to the MainActivity I am trying to use LocalBroadCastManager, using tutorials I have created a broadcast receiver in MainActivity and I am trying to send the broadcast from my fragment however the line
为了将选定日期的字符串发送到MainActivity我尝试使用LocalBroadCastManager,使用教程我在MainActivity中创建了一个广播接收器,我试图从我的片段发送广播但是线路
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
in my Fragment class is underlined with the error 在我的Fragment类中带有错误的下划线
"Error:(51, 30) error: method getInstance in class LocalBroadcastManager cannot be applied to given types; required: Context found: CalendarDatePickerFragment reason: actual argument CalendarDatePickerFragment cannot be converted to Context by method invocation conversion" “错误:(51,30)错误:类LocalBroadcastManager中的方法getInstance无法应用于给定类型;必需:找到上下文:CalendarDatePickerFragment原因:实际参数CalendarDatePickerFragment无法通过方法调用转换转换为Context”
I have tried replacing the "this" with a few other things, such as "Context", however they all give me errors. 我尝试用其他一些东西替换“this”,比如“Context”,但是它们都给我错误。 Here is all of my code minus the imports:
这是我的所有代码减去导入:
MainActivity: 主要活动:
public class MainActivity extends ActionBarActivity implements
View.OnClickListener {
TextView mainTextView;
Button mainButton;
EditText inputbox;
static String DateInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainTextView = (TextView) findViewById(R.id.main_textview);
mainTextView.setText("Set in Java!");
mainButton = (Button) findViewById(R.id.TimeInpButton);
mainButton.setOnClickListener(this);
inputbox = (EditText) findViewById(R.id.inputBox);
// Register to receive messages. We are registering an observer (mMessageReceiver) to receive Intents with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("set-date"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String setdate = intent.getStringExtra("input-date");
Log.d("receiver", "Got message: " + setdate);
inputbox.setText(setdate);
}
};
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public void onClick(View v) {
DialogFragment DatePickerFrag = new CalendarDatePickerFragment();
DatePickerFrag.show(getSupportFragmentManager(), "datePicker");
}
}
CalendarDatePickerFragment: CalendarDatePickerFragment:
public class CalendarDatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
String compileddate = (dd+" "+mm+" "+yy);
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("set-date");
// You can also include some extra data.
intent.putExtra("input-date", compileddate);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Sorry if I have overcomplicated a very simple question, I felt that it was possible I might be doing something wrong on a larger scale so I thought it would be good to make it clear what I was trying to acheive. 对不起,如果我过于复杂化一个非常简单的问题,我觉得我可能会在更大范围内做错事,所以我认为明确我想要实现的目标会很好。
Thank you for any help you can provide. 感谢您提供任何帮助。
Error:(51, 30) error: method getInstance in class LocalBroadcastManager cannot be applied to given types;
错误:(51,30)错误:类LocalBroadcastManager中的方法getInstance无法应用于给定类型; required: Context found: CalendarDatePickerFragment reason: actual argument CalendarDatePickerFragment cannot be converted to Context by method invocation conversion
required:找到的上下文:CalendarDatePickerFragment原因:实际参数CalendarDatePickerFragment无法通过方法调用转换转换为Context
yes, that's correct. 对,那是正确的。
Fragment
does not inherit from Context
, while Activity
does. Fragment
不会从Context
继承,而Activity
也会继承。 That's the reason why you can use this in the Activity
but not in the DialogFragment
subclass. 这就是为什么你可以在
Activity
使用它而不是在DialogFragment
子类中使用它的原因。 Use getActivity()
, in your DialogFragment subclass 在DialogFragment子类中使用
getActivity()
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.