简体   繁体   中英

Start activity in BroadCast Receiver causing app crash

I have created an application in which when I turn on bluetooth a toast is shown and a new activity starts. This is my broadcast receiver class:

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) {

            }
        }
    }
}

Now there is no problem. The activity is starting but in the above code when I put

Indicators.on.setVisibility(View.VISIBLE);

And run the app, It crashes!

Actually on is a textview obj which I have defined in Indicators class as follows:

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

How should I remove this error?

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

And in your BroadcastReceiver:

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

Put this line

                on.setVisibility(View.VISIBLE);

Inside the Activity -> onCreate() method.

Do not use static references to the Activity class members like TextViews from outside the Activity itself as it might have been destroyed, or not have been created yet. This is bad practice in general.

Edit: Add an extra to the Activity starter intent if you need a flag to show the indicator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM