拖动Button(空对象引用)时出错,但我没有任何拖动方法

时间:2018-01-09 12:36:38

标签: android nullpointerexception

我的应用中有多个按钮。现在我改变了其中一些样式,如果我拖动(我没有配置拖动事件),我收到此错误(请参阅下面的堆栈跟踪)。

这是我在样式文件中添加的新样式:

<style name="Button.Raised" parent="@style/Widget.AppCompat.Button.Colored">
    <item name="android:height">36dp</item>
    <item name="android:textSize">14sp</item>
    <item name="android:capitalize">characters</item>
    <item name="android:minWidth">64dp</item>
    <item name="android:paddingRight">8dp</item>
    <item name="android:paddingLeft">8dp</item>
    <item name="android:layout_marginLeft">8dp</item>
    <item name="android:layout_marginRight">8dp</item>
</style>

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.company.appname, PID: 20408
                  java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.Editor$SelectionModifierCursorController.isDragAcceleratorActive()' on a null object reference
                      at android.widget.Editor.updateFloatingToolbarVisibility(Editor.java:1556)
                      at android.widget.Editor.onTouchEvent(Editor.java:1502)
                      at android.widget.TextView.onTouchEvent(TextView.java:10071)
                      at android.view.View.dispatchTouchEvent(View.java:10779)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                      at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:582)
                      at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1885)
                      at android.app.Activity.dispatchTouchEvent(Activity.java:3240)
                      at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:544)
                      at android.view.View.dispatchPointerEvent(View.java:11008)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5162)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5014)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4539)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4592)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4558)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4691)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4566)
                      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4748)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4539)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4592)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4558)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4566)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4539)
                      at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7099)
                      at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7031)
                      at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6992)
                      at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7209)
                      at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                      at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
                      at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
                      at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7173)
                      at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7236)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:734)
                      at android.view.Choreographer.doFrame(Choreographer.java:664)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6776)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

这很烦人,因为即使按钮​​没有附加事件监听器也会失败(只需在活动中放一个按钮并尝试拖动它),应用程序崩溃。我知道我做错了什么?

修改

过滤来源:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.realizar_pago);

    // HERE IS ONE OF THE FAILING BUTTONS
    mostrarPIN = (Button)findViewById(R.id.botonMostrarPIN);
    mostrarPIN.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // ... some stuff here ...
        }
    });

    // The other failing button has no code, no link only was put in 
    // order to check error. That button is called botonFinanciar in XML Code
}

完整来源:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.realizar_pago);

    Bundle b = getIntent().getExtras();
    this.importePago = -1; // or other values
    if (b != null){
        this.importePago = b.getInt("importe");
        this.operacion = b.getString("operacion");
    }

    this.listaTarjetas = new ArrayList<TarjetaCredito>();
    this.listaTarjetas = LocalStorageServices.DatabaseService().getTarjetasCredito();
    //DATOS DE PRUEBA DE TARJETAS
    /*
    addTarjeta(1,"VISA Oro", "asdib23rg34", "7294", false);
    addTarjeta(2, "Compras Online", "fibsdijvbasldjv", "4873", false);
    addTarjeta(3, "Tarjeta familia", "534534j5bn54", "5826", true);
    */
    listaTarjetas.add(new TarjetaCredito());

    // Listado de tarjetas y controles asociados a cada una
    RecyclerView rv = (RecyclerView)findViewById(R.id.realizar_pago_tarjetero);
    rv.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    rv.setLayoutManager(llm);
    adapter = new RVAdapter(this, ponerTarjetaPorDefectoDePrimera(listaTarjetas));
    adapter.setOnRecycleViewClickListener(new RecyclerViewClickListener() {
        @Override
        public void recyclerViewListClicked(View v, int position) {
        TarjetaCredito auxT = listaTarjetas.get(position);
        if(tarjetaSeleccionada == null){
            // NO habia tarjeta seleccionamos la pulsada
            tarjetaSeleccionada = auxT;
            adapter.seleccionarTarjeta(position);
        }else if(tarjetaSeleccionada == auxT){
            // Se ha pulsado sobre la misma tarjeta
            tarjetaSeleccionada = null;
            adapter.deseleccionarTarjeta(position);
        }else{
            // Se ha seleccionado otra tarjeta
            adapter.limpiarSeleccionTarjetas();
            tarjetaSeleccionada = auxT;
            adapter.seleccionarTarjeta(position);
        }
        }
    });
    rv.setAdapter(adapter);

    // Vamos a agregar un observador al adaptador para que lance un evento cuando cambien
    // los datos, es decir, cuando hayan borrado una tarjeta
    adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            adapter.seleccionPorDefecto();
        }
    });
    rv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
        //En este punto la vista ha sido construida y podemos marcar la tarjeta por defecto

        //Seleccionamos la tarjeta por defecto
        adapter.seleccionPorDefecto();
        }
    });

    mostrarPIN = (Button)findViewById(R.id.botonMostrarPIN);
    mostrarPIN.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
        final Dialog dialog = new Dialog(context);

        // Verificamos que haya una tarjeta seleccionada
        if(tarjetaSeleccionada != null){
            // Si hay una tarjeta seleccionada tenemos que pedir el PIN al usuario
            // Si no lo ha establecido, en su primera compra le obligamos a establecer un PIN
            if(LocalStorageServices.DatabaseService().userHasSetPin()){
                // Ha establecido PIN, pedimos que lo ponga
            } else {
                // Proceso de selección de PIN
            }
            dialog.setContentView(R.layout.ventana_pin);
            dialog.setTitle("Inserta tu PIN");
            final EditText pinText = (EditText)dialog.findViewById(R.id.textPin);

            Button dialogButtonPin = (Button) dialog.findViewById(R.id.botonDialogoComprobarPin);
            Button dialogButtonCancel = (Button) dialog.findViewById(R.id.botonDialogoCancelar);
            dialogButtonPin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialogButtonCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });

            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            dialog.show();
            // Ponemos el foco en la caja del pin y abrimos el teclado
            pinText.requestFocus();
        }else{
            // Mostramos diálogo de confirmación
            AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AppThemeDialogs);
            builder.setIcon(android.R.drawable.ic_dialog_alert);
            builder.setTitle("Error de tarjeta");
            builder.setMessage("Para realizar un pago tienes que seleccionar una tarjeta.");
            builder.setPositiveButton("Aceptar", null);
            builder.show();
        }
        }
    });
}

尚未完成。无论如何,当我将样式Button.Raised放到按钮上并长按然后拖动它时会出现错误。

活动的XML:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fondo_pantalla"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textoTitulo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/white_title"
            android:gravity="center"
            android:text="SELECCIONA TU TARJETA"
            android:textAlignment="center"
            style="@style/Typos.Headline"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/realizar_pago_tarjetero"
                android:scrollbars="none"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/barraChat"
                android:orientation="horizontal"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp">

            <TextView
                android:id="@+id/textoImporte"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/white_title"
                android:gravity="center"
                android:text="7€"
                android:textAlignment="center"
                style="@style/Typos.Headline"/>

            <Button
                android:id="@+id/botonMostrarPIN"
                style="@style/Button.Raised"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               android:layout_marginTop="@dimen/activity_vertical_margin"
                android:text="Pagar ya" />
        </LinearLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp">

            <Button
                android:id="@+id/botonFinanciar"
                style="@style/Button.Raised.BlueAbanca"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Financiar"
                android:backgroundTint="#ff5b87da"
                app:backgroundTint="#ff5b87da"
                app:layout_constraintBottom_toTopOf="@+id/imageView4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/imageView4"
                android:layout_width="139dp"
                android:layout_height="28dp"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:visibility="visible"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:srcCompat="@drawable/ic_abanca" />

        </android.support.constraint.ConstraintLayout>
    </LinearLayout>

1 个答案:

答案 0 :(得分:1)

这是一个奇怪的问题,但也许您应该尝试使用clean和rebuild项目并检查字段的初始化