2014-02-27 44 views
0

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

这里是我的类:

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

回答

1

你的类扩展BroadcastReceiverfindViewByIdActivity类的方法。

您可以在活动中注册BroadCast。然后做一切必要的活动类本身

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

你可以让你BroadCasterReceiver作为一个内部类Activity类的

您可以注销在

或者

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

使用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") 

然后终于在你的活动实现的接口。

public class MainActivity extends Activity implements BReceiver{ 

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

} 
0

把ID成束,并从那里

+0

如果我这样做,它说,该型广播接收器不能Activity_Sec的超接口;一个超级接口必须是一个接口。 – user3359953

0

我的建议是使用它:拿出所有的东西OnClickListener。只需声明一个处理函数来实现正确的接口并将其链接到XML中。

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

然后,在你的布局/ your_activity.XML文件:

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

它仍然说该方法findViewById(int)未定义类型和handleAmbulanceClick无法解析为一个类型。 – user3359953

+0

我认为http://stackoverflow.com/users/653856/raghunandan已经确定了答案:你应该让你的课程成为一个活动。 – Sparky