从DialogFragment调用registerReceiver

时间:2012-09-03 05:42:11

标签: android fragment receiver

修改

我添加了这一行:

getActivity().registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));

现在有一个运行时错误,错误如下:

09-03 04:12:41.856:E / AndroidRuntime(1692):java.lang.IllegalStateException:在onClick处理程序的活动类android.view.ContextThemeWrapper中找不到方法sendMessage(View)查看id为“button_send”

的类android.widget.ImageView

我的问题是:为什么要在 android.view.ContextThemeWrapper 中寻找方法?

按钮是在我们在线引用的布局上创建的:

View view = inflater.inflate(R.layout.activity_composer, container);

并且“button_send”将onClick方法设置为:

android:onClick="sendMessage"

以后定义为:

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

单击发送消息的按钮时会发生运行时错误。

END OF EDIT

编辑二号

显然,您无法在XML中定义onClick方法,因为该按钮是片段的一部分。在这种情况下,我必须设置onClickListener

问题FIXED,谢谢!

所以我试图创建一个只发送新短信的DialogFragment。 Dialog有两个EditText视图,一个用于电话号码,另一个用于实际消息;它还有1个ImageButton,它有一个名为sendMessage(View v)的onClick方法。话虽如此,我在注册接收器时遇到问题,以便在发送消息时捕获结果代码。错误消息是:

“方法registerReceiver(new BroadcastReceiver(){},IntentFilter)未定义类型ComposerDialog”

使用完全相同的代码块,除了onCreateView()之外,我可以在Activity上进行此工作,但显然不在 DialogFragment 上。我的猜测是在线:

PendingIntent sentPI = PendingIntent.getBroadcast(getActivity(), 0,
                new Intent(SENT), 0);

通过调用getActivity(),我将一个Dialog作为 context 传递,显然这不是一个对registerReceiver的调用在线识别的参数:

registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));

我正在考虑如何更改传递给registerReceiver调用的上下文,但我甚至不确定这实际上是什么问题。有什么想法吗?

以下是更好的观点的完整类:

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

}

2 个答案:

答案 0 :(得分:16)

使用

getActivity().registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));

registerReceiver是Context的一种方法,在DialogFragment

中不存在

答案 1 :(得分:0)

执行类似

的操作
@Override
    public void onResume() {
        super.onResume();
        updateUiState();
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(progressReceiver,new IntentFilter(EPackDownloader.ACTION_PROGRESS));
    }

    @Override
    public void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(progressReceiver);
    }

并发送广播。这是从具有应用程序上下文的AsyncTask发送的

private void updateProgress() {
    //publish progress
    int progress = 100*(totalImageCount - (products.size() * 3 - (3 - (int)downLatch.getCount())))/totalImageCount;
    publishProgress(progress);
}

@Override
protected void onProgressUpdate(Integer... values) {
    Intent data = new Intent(ACTION_PROGRESS);
    data.putExtra("progress",values[0]);
    data.putExtra("ePackId",ePackModel.id);
    LocalBroadcastManager.getInstance(context).sendBroadcast(data);
}