用Log
class跟踪运行时显示onReceive()
方法没有调用,为什么?
动态注册广播接收器
private void discoverDevices () {
Log.e("MOHAB","BEFORE ON RECEIVE");
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.e("MOHAB","ON RECEIVE");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
Bluetooth b = new Bluetooth(device.getName(),device.getAddress());
list.add(b);
}
}
};
Log.e("MOHAB","create intentFilter");
// …
Run Code Online (Sandbox Code Playgroud) 我LinearLayout
在屏幕旋转后返回对象返回的nullPointerException异常, 虽然我在onCreate()
super.onCreate(null)中传递null ; .我知道Activity必须被销毁并重新创建旁边我传递的savedInstanceState = null表示Activity应该在旋转后开始,因为它是第一次启动,为什么我在轮换后得到这个Exception?
onCreate()片段,其中LinearLayout
对象名为historyText
LinearLayout historyText ; // this return exception if it used after rotation .
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
setContentView(R.layout.activity_main);
historyText = (LinearLayout) findViewById(R.id.historyLayoutText);
Log.e("HISTORYVISIBILITY", "VISIBILITY = "+historyText.getVisibility());
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
database = new Database(this);
Bundle extras = getIntent().getExtras();
intent = extras.getInt("activity");
p = new Progress();
getSupportFragmentManager().beginTransaction()
.add(R.id.group, p)
.commit();
orientation = getRotation();
switch (orientation){
case "p" :
addPlus();
}
}
Run Code Online (Sandbox Code Playgroud)
logcat的
10-07 22:21:24.365: E/AndroidRuntime(7768): …
Run Code Online (Sandbox Code Playgroud) android nullpointerexception screen-rotation android-linearlayout
android ×2