从DialogFragment调用registerReceiver [英] Calling registerReceiver from a DialogFragment
问题描述
修改
我添加这一行:
getActivity().registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));
有现在虽然运行时错误,错误内容如下:
There is now a runtime error though, the error reads:
四月九日至3日:12:41.856:E / AndroidRuntime(1692):java.lang.IllegalStateException:在活动课android.view.ContextThemeWrapper为的onClick处理程序无法找到一个方法的sendMessage(查看)上视图类android.widget.ImageView id为button_send
我的问题是:为什么会寻找该方法的 android.view.ContextThemeWrapper
My question would be: Why would it be looking for the method in android.view.ContextThemeWrapper?
该按钮的布局创建我们的行引用:
The button is created on the layout we referenced on line:
View view = inflater.inflate(R.layout.activity_composer, container);
和button_send有一个onClick方法设置为:
and the "button_send" has an onClick method set as:
android:onClick="sendMessage"
这就是后来的定义为:
Which is later defined as:
public void sendMessage (View v) {
String phoneNumber = number_input.getText().toString();
String message = body_input.getText().toString();
if(message.length() > 0) {
sendSMS(phoneNumber, message);
}
sendSMS(phoneNumber, message);
clearForm();
}
点击发送消息的按钮时,运行时错误发生。
The runtime error happens when the button to send the message is clicked.
编辑END
修改二号
显然,因为按钮是片段的一部分,你无法定义XML里面的onClick方法。在这种情况下,我必须设置一个onClickListener
Apparently you cannot define the onClick method inside your XML since the button is part of the fragment. In this case, I had to set an onClickListener
发出定额,谢谢!
所以我想创建一个简单的发送一个新的文本消息DialogFragment。对话框有两个的EditText观点,一个用于电话号码,以及一个用于实际消息;它还具有1的ImageButton有一个onClick方法调用的sendMessage(视图V)。随着中说,我有登记接收器捕捉到的结果code正在发送邮件时的问题。错误消息是:
So I am trying to create a DialogFragment that simply sends a new text message. The Dialog has two EditText views, one for the phone number, and one for the actual message; it also has 1 ImageButton that has an onClick method called sendMessage (View v). With that said, I am having issues registering a receiver to catch the result code when the message is being sent. The error message is:
的方法registerReceiver(新广播接收器(){},IntentFilter的)是未定义的类型ComposerDialog
"The method registerReceiver(new BroadcastReceiver(){}, IntentFilter) is undefined for the type ComposerDialog"
使用完全相同code的相同的块,除onCreateView(),我可以在一个活动这项工作,但显然不是在 DialogFragment 。我的猜测是,就行了:
With exactly the same blocks of code, excepting onCreateView(), I can make this work on an Activity, but apparently not on a DialogFragment. My guess is that on the line:
PendingIntent sentPI = PendingIntent.getBroadcast(getActivity(), 0,
new Intent(SENT), 0);
通过调用getActivity(),我传递一个对话框,在背景,显然这不是一个参数调用registerReceiver在网上认识:
by calling getActivity(), I am passing a Dialog as the context, and apparently that is not a parameter the call to registerReceiver recognizes on line:
registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));
我想到的是如何改变我传递给registerReceiver通话的背景下,但我什至不知道,实际上是这个问题。任何想法?
I am thinking of how to change the context I pass to the registerReceiver call, but I am not even sure that is actually the problem. Any ideas?
下面是一个更好的角度完整的类:
Here is the full Class for a better perspective:
package com.deadpixels.test.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class ComposerDialog extends DialogFragment {
public ComposerDialog () {
}
private EditText body_input;
private EditText number_input;
SmsManager smsMgr = SmsManager.getDefault();
private static final String TAG = "Composer";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_composer, container);
body_input = (EditText) view.findViewById(R.id.text_input);
number_input = (EditText) view.findViewById(R.id.text_address);
getDialog().setTitle("Create your new Message!");
return view;
}
private void clearForm() {
body_input.setText("");
number_input.setText("");
}
public void sendMessage (View v) {
String phoneNumber = number_input.getText().toString();
String message = body_input.getText().toString();
if(message.length() > 0) {
sendSMS(phoneNumber, message);
}
sendSMS(phoneNumber, message);
clearForm();
}
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(getActivity(), 0,
new Intent(SENT), 0);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Log.v(TAG, "SMS sent succesfully");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.v(TAG, "Generic failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.v(TAG, "No service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Log.v(TAG, "Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.v(TAG, "Radio off");
break;
}
}
}, new IntentFilter(SENT));
smsMgr.sendTextMessage(phoneNumber, null, message, sentPI, null);
clearForm();
}
}
推荐答案
使用
getActivity().registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));
registerReceiver
是上下文的方法,它是不是在DialogFragment present
registerReceiver
is a method of Context which is not present in DialogFragment
这篇关于从DialogFragment调用registerReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!