[英]textView NullPointException
package com.example.helloworld;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.BindView;
import butterknife.OnClick;
public class CallingService extends Service {
public static final String EXTRA_CALL_NUMBER = "call_number";
protected View rootView;
@BindView(R.id.tv_call_number)
TextView tv_call_number;
String call_number;
WindowManager.LayoutParams params;
private WindowManager windowManager;
@Override
public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
int width = 0;
if(Build.VERSION.SDK_INT>12){
Point size = new Point();
display.getSize(size);
width = (int) (size.x * 0.9);
}else{
int x = display.getWidth();
width = (int) (x * 0.9);
}
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
params = new WindowManager.LayoutParams(
LAYOUT_FLAG,
WindowManager.LayoutParams.WRAP_CONTENT,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
: WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
PixelFormat.TRANSLUCENT);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
rootView = layoutInflater.inflate(R.layout.call_popup_top, null);
ButterKnife.bind(this, rootView);
setDraggable();
}
private void setDraggable() {
rootView.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:
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
if (rootView != null)
windowManager.updateViewLayout(rootView, params);
return true;
}
return false;
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
windowManager.addView(rootView, params);
setExtra(intent);
if (!TextUtils.isEmpty(call_number)) {
tv_call_number.setText(call_number);
}
return START_REDELIVER_INTENT;
}
private void setExtra(Intent intent) {
if (intent == null) {
removePopup();
return;
}
call_number = intent.getStringExtra(EXTRA_CALL_NUMBER);
}
@Override
public void onDestroy() {
super.onDestroy();
removePopup();
}
@OnClick(R.id.btn_close)
public void removePopup() {
if (rootView != null && windowManager != null) windowManager.removeView(rootView);
}
}
在 onStartCommand 方法中,
if (!TextUtils.isEmpty(call_number)) {
tv_call_number.setText(call_number);
}
setText 方法會出錯
java.lang.RuntimeException: Unable to start service com.example.helloworld.CallingService@3fedd36 with Intent { cmp=com.example.helloworld/.CallingService (has extras) }: java.lang.NullPointerException: Attempt to invoke virtual method ' void android.widget.TextView.setText(java.lang.CharSequence)' 在 null ZA8CFDE6331BD4B6669
我試圖讓它在傳入屏幕上顯示傳入號碼。 我將此服務綁定在廣播接收器中。
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
final String phone_number;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
phone_number = PhoneNumberUtils.formatNumber(incomingNumber, Locale.getDefault().getCountry());
} else {
phone_number = PhoneNumberUtils.formatNumber(incomingNumber);
}
Intent serviceIntent = new Intent(context, CallingService.class);
serviceIntent.putExtra(CallingService.EXTRA_CALL_NUMBER, phone_number);
context.startService(serviceIntent);
}
},PhoneStateListener.LISTEN_CALL_STATE);
我檢查了 phone_number 以正確的方式傳遞,並且進展順利。 這段代碼有什么問題?
unbinder = ButterKnife.bind(this);
將此行代碼添加到您的 onCreate 方法中。 這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.