简体   繁体   中英

java.lang.NullPointerException in Android Webservice

me again.

I am having a little issue with my android app.

When I run the service on the phone, it starts just fine and when the call is disconnected it throws an java.lang.NullPointerException when i start the tareaEnviarABD.execute(); task.

This is the code of my service:

package com.trucka.llamadasdrivers;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.IBinder;
import android.provider.CallLog;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class ServicioLlamadas extends Service {

    TextView txtInformacion = null;
    TextView txtDetalles = null;
    TextView tv = null;
    // Comunicación con el webservice.
    private final String NAMESPACE = "http://truckanet.com/MensajeOperador";
    // private final String URL =
    private final String URL = "http://192.168.10.94/MensajeOperador/MensajeOperador.asmx";
    //private final String URL = "http://200.76.187.148/MensajeOperador/MensajeOperador.asmx";
    private final String SOAP_ACTION = "http://truckanet.com/MensajeOperador/ActualizarFede";
    private final String METHOD_NAME = "ActualizarFede";
    private String TAG = "TRUCKA_DRIVERS";
    private String resultado;
    String phNumber = null;
    String callType = null;
    String callDate = null;
    DateFormat shortFecha = null;
    DateFormat shortDF = null;
    Date callDayTime = null;
    Date fin = null;
    String fechaLlamada1 = null;
    String fechaLlamada2 = null;
    String callDuration = null;
    String dir = null;
    public String tolo = null;
    String imei = null;
    String comentario = null;
    String fechaRegistro = null;
    String insercion = null;
    String fechaInicio = null;
    String fechaFin = null;
    String estadoLlamada = null;

    private static ServicioLlamadas instance = null;

    public static boolean isRunning() {
        return instance != null;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(getApplicationContext(), "Servicio TRUCKA creado",
                Toast.LENGTH_SHORT).show();
        instance = this;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(getApplicationContext(), "Servicio TRUCKA destruído",
                Toast.LENGTH_SHORT).show();
        instance = null;
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(getApplicationContext(), "Servicio TRUCKA iniciado",
                Toast.LENGTH_SHORT).show();
        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyMgr.listen(new TeleListener(),
                PhoneStateListener.LISTEN_CALL_STATE);
    }

class TeleListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                if (estadoLlamada == "OFFHOOK") {
                    insercion = null;
                    getCallDetails();
                    Toast.makeText(getApplicationContext(),
                            "Información enviada.", Toast.LENGTH_SHORT).show();
                }
                estadoLlamada = "IDLE";
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                estadoLlamada = "OFFHOOK";
                Toast.makeText(getApplicationContext(),
                        "Enviando información...", Toast.LENGTH_SHORT).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                break;
            default:
                break;
            }
        }
    }

//Obtener la fecha actual del teléfono.
    public long getTodayTimestamp() {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date());

        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
        c2.set(Calendar.MONTH, c1.get(Calendar.MONTH));
        c2.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
        c2.set(Calendar.HOUR_OF_DAY, 0);
        c2.set(Calendar.MINUTE, 0);
        c2.set(Calendar.SECOND, 0);

        return c2.getTimeInMillis();
    }

private void getCallDetails() {
    String timestamp = String.valueOf(getTodayTimestamp());
    StringBuffer sb = new StringBuffer();
    Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
            CallLog.Calls.DATE + ">= ?", new String[] { timestamp }, null);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Bitácora de llamadas :");
     //managedCursor.moveToFirst();
    managedCursor.moveToLast();
    phNumber = managedCursor.getString(number);
    callType = managedCursor.getString(type);
    callDate = managedCursor.getString(date);
    shortFecha = DateFormat.getDateInstance(DateFormat.SHORT);
    shortDF = DateFormat.getTimeInstance(DateFormat.SHORT);
    callDayTime = new Date(Long.valueOf(callDate));
    fechaLlamada1 = shortDF.format(callDayTime);
    fechaLlamada2 = shortFecha.format(callDayTime);
    callDuration = managedCursor.getString(duration);
    int dircode = Integer.parseInt(callType);
    TelephonyManager mngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    switch (dircode) {
    case CallLog.Calls.OUTGOING_TYPE:
        dir = "Saliente";
        break;

    case CallLog.Calls.INCOMING_TYPE:
        dir = "Entrante";
        break;

    case CallLog.Calls.MISSED_TYPE:
        dir = "Perdida";
        break;
    }

    imei = mngr.getDeviceId();
    comentario = dir;
    fechaRegistro = fechaLlamada2;
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    String ahorita = df.format(callDayTime);
    fechaInicio = ahorita.toString();

    insercion = "DECLARE @claveDriver INT, @nombreDriver VARCHAR(max), @evento VARCHAR(max), @duracion int, @inicial varchar(max) "
            + "SET @claveDriver = (SELECT cve_tra FROM SISTEMA.dbo.TELEFONOS WHERE IMEI_SIM = '"
            + mngr.getDeviceId()
            + "') "
            + "SET @nombreDriver = (SELECT nombre FROM SISTEMA.dbo.TELEFONOS WHERE IMEI_SIM = '"
            + mngr.getDeviceId()
            + "') "
            + "SET @duracion = "
            + managedCursor.getString(duration)
            + "SET @evento = '(LOG) Llamada "
            + dir
            + ". Duración ' + CONVERT(varchar, @duracion, 103) + ' segundos al número: "
            + managedCursor.getString(number)
            + "' "
            + " SET @inicial = '"
            + fechaInicio
            + "'"
            + "INSERT INTO bitacora.dbo.registroDellamadasOperadores (fechacreacion,fecha_fin,fecha_inicio,idMobil,Tractor,Nom_tra,Cve_tra,FechaRegistro,Evento) "
            + " VALUES('"
            + fechaInicio
            + "', DATEADD(SECOND,@duracion,@inicial),'"
            + fechaInicio
            + "','"
            + mngr.getDeviceId()
            + "','',@nombreDriver,@claveDriver,current_timestamp,@evento)";
    try
    {
    AsyncCallWS tareaEnviarABD = new AsyncCallWS();
    tareaEnviarABD.execute();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(),
                e.toString(), Toast.LENGTH_SHORT).show();
    }
}

private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        Log.i(TAG, "doInBackground");
        InsertarLlamada(insercion);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "onPostExecute");
        txtInformacion.setText("Información enviada");
    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
        txtInformacion.setText("Enviando información...");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }
}
public void InsertarLlamada(String insercion) {
    // Creamos la solicitud
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    // Propiedades que contienen los valores
    PropertyInfo propiedades = new PropertyInfo();

    propiedades.setName("insercion");
    propiedades.setValue(insercion);
    propiedades.setType(String.class);
    // Agregamos las propiedades
    request.addProperty(propiedades);
    // Creamos el envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    // ponemos la salida SOAP
    envelope.setOutputSoapObject(request);
    // Creamos la llamada HTTP
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        // Invocamos el servicio
        androidHttpTransport.call(SOAP_ACTION, envelope);
        // Obtenemos la respuesta
        Object response = envelope.getResponse();
        // Asignamos el resultado de la consulta
        resultado = response.toString();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                e.toString(), Toast.LENGTH_SHORT).show();
    }
}
}

And the log cat shows this:

02-25 09:32:32.946: D/ActivityThread(8866): setTargetHeapUtilization:0.25
02-25 09:32:32.946: D/ActivityThread(8866): setTargetHeapIdealFree:8388608
02-25 09:32:32.946: D/ActivityThread(8866): setTargetHeapConcurrentStart:2097152
02-25 09:33:07.663: D/libEGL(8866): loaded /system/lib/egl/libEGL_adreno200.so
02-25 09:33:07.663: D/libEGL(8866): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
02-25 09:33:07.663: D/libEGL(8866): loaded /system/lib/egl/libGLESv2_adreno200.so
02-25 09:33:07.683: I/Adreno200-EGLSUB(8866): <ConfigWindowMatch:2087>: Format RGBA_8888.
02-25 09:33:07.693: E/(8866): <s3dReadConfigFile:75>: Can't open file for reading
02-25 09:33:07.693: E/(8866): <s3dReadConfigFile:75>: Can't open file for reading
02-25 09:33:07.693: D/OpenGLRenderer(8866): Enabling debug mode 0
02-25 09:33:13.128: I/TRUCKA_DRIVERS(8866): onPreExecute
02-25 09:33:13.128: D/AndroidRuntime(8866): Shutting down VM
02-25 09:33:13.128: W/dalvikvm(8866): threadid=1: thread exiting with uncaught exception (group=0x41e48438)
02-25 09:33:13.128: E/AndroidRuntime(8866): FATAL EXCEPTION: main
02-25 09:33:13.128: E/AndroidRuntime(8866): java.lang.NullPointerException
02-25 09:33:13.128: E/AndroidRuntime(8866):     at com.trucka.llamadasdrivers.ServicioLlamadas$AsyncCallWS.onPreExecute(ServicioLlamadas.java:239)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at android.os.AsyncTask.execute(AsyncTask.java:534)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at com.trucka.llamadasdrivers.ServicioLlamadas.getCallDetails(ServicioLlamadas.java:215)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at com.trucka.llamadasdrivers.ServicioLlamadas.access$0(ServicioLlamadas.java:143)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at com.trucka.llamadasdrivers.ServicioLlamadas$TeleListener.onCallStateChanged(ServicioLlamadas.java:108)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:421)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at android.os.Looper.loop(Looper.java:137)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at android.app.ActivityThread.main(ActivityThread.java:5062)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at java.lang.reflect.Method.invokeNative(Native Method)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at java.lang.reflect.Method.invoke(Method.java:511)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-25 09:33:13.128: E/AndroidRuntime(8866):     at dalvik.system.NativeStart.main(Native Method)
02-25 09:33:14.240: I/Process(8866): Sending signal. PID: 8866 SIG: 9

Can someone please tell me what am I doing wrong? :(

Thanks!

You have never initialized txtInformacion.

TextView txtInformacion = null;

So you get NPE at:

@Override
protected void onPreExecute() {
    Log.i(TAG, "onPreExecute");
    txtInformacion.setText("Enviando información...");
}

You are not initializing txtInformacion anywhere in your code. You need to initialize it with the appropriate TextView before you can use it in onPreExecute() method of AsyncCallWS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM