【问题标题】:findViewById is undefined for the type errorfindViewById 未定义类型错误
【发布时间】:2014-02-27 13:51:29
【问题描述】:

当我将它用于按钮时,我收到 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); }
    }
}

【问题讨论】:

    标签: android eclipse button android-broadcast findviewbyid


    【解决方案1】:

    您的类扩展了BroadcastReceiver,而findViewByIdActivity 类的方法。

    您可以在活动中注册广播。然后在 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
    
    }
    

    在接收中

    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
    
    }
    

    【讨论】:

      【解决方案2】:

      我的建议:去掉所有 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" />
      

      【讨论】:

      • 它仍然说 The method findViewById(int) is undefined for the type and handleAmbulanceClick cannot be resolve to a type.
      • 我认为stackoverflow.com/users/653856/raghunandan 已经确定了答案:你应该让你的班级成为一个活动。
      【解决方案3】:

      将 id 放入 bundle 并从那里使用它

      【讨论】:

      • 如果我这样做,它会说广播接收器类型不能是 Activity_Sec 的超接口;超接口必须是接口。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2019-10-31
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      相关资源
      最近更新 更多