【问题标题】:textView NullPointExceptiontextView 空指针异常
【发布时间】:2021-05-17 23:29:29
【问题描述】:
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: 尝试调用空对象引用上的虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

我试图让它在来电屏幕上显示来电号码。我在广播接收器中绑定了这个服务。

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,它运行良好。 这段代码有什么问题?

【问题讨论】:

    标签: java android-studio nullpointerexception textview


    【解决方案1】:

    unbinder = ButterKnife.bind(this);

    将此行代码添加到您的 onCreate 方法中。这会有所帮助。

    【讨论】:

    • 它是如何工作的?我不明白,因为它只是添加新变量
    • 我认为它发生在视图绑定上。
    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多