[英]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: 其实
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);
}
}
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: 在您的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. 在Activity中-> onCreate()方法。
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. 不要从Activity本身外部使用对Text类的Activity类成员的静态引用,因为它可能已被破坏或尚未创建。 This is bad practice in general.
一般来说,这是不好的做法。
Edit: Add an extra to the Activity starter intent if you need a flag to show the indicator. 编辑:如果您需要一个标志来显示指标,请向“活动初学者”意图添加其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.