findViewById未定义类型错误

时间:2014-02-27 13:51:30

标签: android eclipse button android-broadcast findviewbyid

当我将它用于Button时,我收到了findViewById错误。我知道该函数是在Activity类中声明的,但是如何在不扩展Activity类的情况下实现该函数。我有另一个扩展Activity的类但我不能将它用于我的Button Activity,因为我需要的变量都在这个Activity中。

这是我的班级:

public class Activity_Sec extends BroadcastReceiver{
    private static final String LOGCAT = null;
    Button b1;
    final SmsManager sms = SmsManager.getDefault();
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();
        try { if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                final MediaPlayer mp = MediaPlayer.create(context,R.raw.hospital_alarm);

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();
                Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);

                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();

                String serverNumber= "+9198xxxxxxx";
                b1 = (Button)findViewById(R.id.button1);
                if(senderNum.equals(serverNumber)){
                    Toast toast1 = Toast.makeText(context,"alert message received!!!!",Toast.LENGTH_LONG);
                    toast1.show();
                    mp.start();
                }
                b1.setOnClickListener(new OnClickListener(){ public void onClick(View v){
                    String phoneNumber = "+9198xxxxxxxx";
                    String message = "Ambulance sent!";
                    mp.stop();
                    mp.release();
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNumber, null, message, null, null);
                }});
            }
        }} catch (Exception e) { Log.e("SmsReceiver", "Exception smsReceiver" +e); }
    }
}

3 个答案:

答案 0 :(得分:1)

您的课程延伸BroadcastReceiverfindViewByIdActivity课程的方法。

您可以在活动中注册BroadCast。然后执行Activity类本身所需的任何内容

Activity_Sec sec = new Activity_Sec()
registerReceiver(broadCastReceiver, new IntentFilter(
        "some Action"));

您可以将BroadCasterReceiver作为Activity类的内部类

您可以在onPause

中取消注册

或者

您可以使用界面作为活动的回调

或者

使用EventBus

https://github.com/greenrobot/EventBus

 public interface BReceiver {

 public void returnData(String value); // note type is string

}

在onReceive

BReceiver br = (BReceiver) context;

然后

br.returnData("your string data")

然后最后在你的Activity中实现界面。

public class MainActivity extends Activity implements BReceiver{

@Override
public void returnData(String value) {
    // TODO Auto-generated method stub

}

答案 1 :(得分:0)

将id放入bundle并从那里使用

答案 2 :(得分:0)

我的建议:取出所有OnClickListener的东西。只需声明一个处理函数,该函数实现正确的接口并将其链接到XML中。

public void handleAmbulanceClick(View v) {
  // ... what's in onClick now ...
}

然后,在你的layout / your_activity.XML文件中:

<Button
  android:id="@+id/button1"
  ... your other atttributes ...
  android:doClick="handleAmbulanceClick" />