在BroadCast Receiver中启动导致应用程序崩溃的活动
我创建了一个应用程序,当我打开蓝牙时,会显示一个Toast并显示一个新的活动。这是我的广播接收机类:在BroadCast Receiver中启动导致应用程序崩溃的活动
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
是我在Indicators class
定义如下一个textview obj
:
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);
}
}
我应该如何消除这种错误?
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;
}
}
而在你的广播接收器:
YOurActivity inst = YOurActivity.instance();
if(inst != null) { // your activity can be seen, and you can update it's context
inst.setViewText...
}
你能告诉我的代码中有什么错误吗? –
我已经使用'Indicators.on.setVisibility(View.VISIBLE);'从另一个类访问'textview'! –
这很可能是您的活动onCreate方法尚未调用此行:on =(TextView)findViewById(R.id.textView2); 因此,此TextView为空。在下面检查我的答案。 – Lev
把这一行
on.setVisibility(View.VISIBLE);
活动内部 - > onCreate()方法。
不要在Activity本身外部使用对Activity类成员(如TextViews)的静态引用,因为它可能已被销毁,或者尚未创建。一般来说这是不好的做法。
编辑:如果您需要标志来显示指标,请在活动起始者意向中添加一个额外内容。
中创建但我想要的是,只要系统广播发生'BluetoothAdapter.state_off',那么只有'on.setVisibility(View.VISIBLE)'应该发生 –
如果我把'on.setVisibility( View.VISIBLE)'in'onCreate()',那么它总是可见的,我不想要。我希望它只在特定广播发生时才可见! –
您可以在您的活动中使用BroadcastReceiver并激发广播,以便Activity可以设置TextView的可见性。此外,还有一个开放源代码库可以实现这一点,代码比BroadcastReceivers简单得多。 https://github.com/greenrobot/EventBus – Lev
请把你的错误stacktrace到你的文章。 – Opiatefuchs