在BroadCast Receiver中启动活动导致应用程序崩溃

时间:2016-09-16 13:12:35

标签: java android bluetooth broadcastreceiver

我创建了一个应用程序,当我打开蓝牙时,会显示一个Toast并开始一个新的活动。这是我的广播接收器类:

public class BroadCast extends BroadcastReceiver {

    String prefs="myPrefs";
    String count="myCount";
    static int counter=0;
    Intent i;

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        String bluth = arg1.getAction();
        if (bluth.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            if(arg1.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON){
                SharedPreferences sp = arg0.getSharedPreferences(prefs, Context.MODE_PRIVATE);
                Editor ed = sp.edit();
                ed.putInt(count, counter);
                ed.commit();
                counter++;
                Toast.makeText(arg0, "Bluetooth on " + sp.getInt(count, 0), Toast.LENGTH_LONG).show();
                i = new Intent(arg0, Indicators.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                arg0.startActivity(i);
                Indicators.on.setVisibility(View.VISIBLE);
            } else if (arg1.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {

            } else if (arg1.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_TURNING_OFF) {

            } else if (arg1.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_TURNING_ON) {

            }
        }
    }
}

现在没有问题。当我放

时,活动开始但在上面的代码中
Indicators.on.setVisibility(View.VISIBLE);

运行应用程序,它崩溃了!

实际上on是我在textview obj中定义的Indicators class,如下所示:

public class Indicators extends Activity {
    static TextView on, off, opening, closing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textviewbluetooth);

        opening = (TextView)findViewById(R.id.textView1);
        on = (TextView)findViewById(R.id.textView2);
        closing = (TextView)findViewById(R.id.textView3);
        off = (TextView)findViewById(R.id.textView4);
        opening.setVisibility(View.INVISIBLE);
        on.setVisibility(View.INVISIBLE);
        off.setVisibility(View.INVISIBLE);
        closing.setVisibility(View.INVISIBLE);
    }
}

我该如何删除此错误?

2 个答案:

答案 0 :(得分:0)

class YourActivity extends xxxx {
   private static YourActivity mInst;

   public static YOurActivity instance() {
             return mInst;
   }

   /// Do your task here.
   public void setViewText(xxxx) ;

   @Override
   public void onStart() {
     ...
     mInst = this;
   }

   @Override
   public void onStop() {
     ...
     mInst = null;
   }
}

在你的BroadcastReceiver中:

YOurActivity inst = YOurActivity.instance();
   if(inst != null)  { // your activity can be seen, and you can update it's context 
       inst.setViewText...
   }

答案 1 :(得分:0)

把这一行

                on.setVisibility(View.VISIBLE);

活动内部 - > onCreate()方法。

不要对Activity之外的Activity类成员使用静态引用,因为它可能已被销毁,或者尚未创建。一般来说,这是不好的做法。

编辑:如果需要一个标志来显示指示符,请在Activity启动器意图中添加一个额外内容。