我是android编程的新手。我的应用程序在WindowManager上得到了NPE,我想删除它。我尝试了mWindowManager.removeView(mFloatingView);但是它给了我一个空指针异常。请帮助!。对不起,我的英语不好。 这是我的广播接收器代码:
public class PhoneStateReceiver extends BroadcastReceiver {
private int mId;
private WindowManager mWindowManager;
private View mFloatingView;
@Override
public void onReceive(final Context context, final Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
final String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
int LAYOUT_FLAG;
mFloatingView = LayoutInflater.from(context).inflate(R.layout.notification_layout, null);
final View collapsedView = mFloatingView.findViewById(R.id.collapse_view);
final TextView number = mFloatingView.findViewById(R.id.tv_phoneNumber);
final TextView info = mFloatingView.findViewById(R.id.tv_info);
final TextView howmany = mFloatingView.findViewById(R.id.tv_howmany);
final Button closeButtonCollapsed = mFloatingView.findViewById(R.id.close_button);
number.setText(incomingNumber);
StringRequest jsonArrayRequest = new StringRequest(Request.Method.POST, Functions.FETCH_DATA_URL, new Response.Listener<String>() {
@SuppressWarnings("LogNotTimber")
@Override
public void onResponse(String response) {
Log.d(TAG, "FETCH DATA RESPONSE FROM RECIEVER: " + response);
for (int i = 0; i < response.length(); i++) {
try {
JSONArray jsonObject = new JSONArray(response);
info.setText(jsonObject.getJSONObject(0).getString("info1"));
String PhoneNumber = jsonObject.getJSONObject(0).getString("phone_number");
howmany.setText(jsonObject.getJSONObject(0).getString("times_contacted"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@SuppressWarnings("LOGNOTTIMBER")
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
}) {
@SuppressWarnings("LogNotTimber")
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("phone_number", incomingNumber);
Log.d(TAG, "SENDING FROM BROADCAST RECEIVER: " + params.toString());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context.getApplicationContext());
requestQueue.add(jsonArrayRequest);
closeButtonCollapsed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mWindowManager.removeView(mFloatingView);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
//Add the view to the window
mWindowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);
//….
//….
mFloatingView.findViewById(R.id.root_container).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
int Xdiff = (int) (event.getRawX() - initialTouchX);
int Ydiff = (int) (event.getRawY() - initialTouchY);
return true;
case MotionEvent.ACTION_MOVE:
//Calculate the X and Y coordinates of the view.
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
mWindowManager.updateViewLayout(mFloatingView, params);
return true;
}
return false;
}
});
}
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Toast.makeText(context,"Call Received State",Toast.LENGTH_LONG).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context,"Call idle State",Toast.LENGTH_LONG).show();
mWindowManager.removeView(mFloatingView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果我按下关闭按钮,则什么也没有发生。但是在调试控制台中,其空指针异常。我想在通话状态为空闲时删除该窗口。