7

编辑

我添加了这一行:

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

但是现在有一个运行时错误,错误显示为:

09-03 04:12:41.856: E/AndroidRuntime(1692): java.lang.IllegalStateException: 在视图类 android.widget 上的 onClick 处理程序的活动类 android.view.ContextThemeWrapper 中找不到方法 sendMessage(View)。带有 id 'button_send' 的 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();
    }

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

编辑结束

编辑二号

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

问题已解决,谢谢!

所以我正在尝试创建一个 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 作为上下文传递,显然这不是对 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();        
    }

}

4

2 回答 2

16

利用

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

registerReceiver是 DialogFragment 中不存在的 Context 方法

于 2012-09-03T05:45:46.523 回答
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);
}
于 2017-09-29T06:09:16.030 回答