1

我的意图服务没有启动。它给出了一个错误。我是安卓新手。我正在使用restful web api。我在 android 设备上使用服务。我创建了一个同步模块,如果没有覆盖区域,应用程序会将数据存储在数据库中,并每 60 秒检查一次网络。

当它上线时,它将在服务器上上传数据。我希望这个过程在后台运行。为此,我使用了 Intent Service,它在后台运行并在执行后通知用户。但是服务没有开始我在网上查看了许多教程,它们都是相同的方法。但我的不工作。

<p><b>Below is my error code</b><p>
    <code> 
    10-11 11:45:30.734: W/dalvikvm(396): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    10-11 11:45:30.774: E/AndroidRuntime(396): FATAL EXCEPTION: main
    10-11 11:45:30.774: E/AndroidRuntime(396): java.lang.RuntimeException: Unable to instantiate service com.remote.synchronizer.haris.OfflineDataService: java.lang.NullPointerException
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.os.Handler.dispatchMessage(Handler.java:99)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.os.Looper.loop(Looper.java:123)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at java.lang.reflect.Method.invokeNative(Native Method)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at java.lang.reflect.Method.invoke(Method.java:507)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at dalvik.system.NativeStart.main(Native Method)
    10-11 11:45:30.774: E/AndroidRuntime(396): Caused by: java.lang.NullPointerException
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.content.ContextWrapper.getSystemService(ContextWrapper.java:363)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:742)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at com.remote.synchronizer.haris.OfflineDataService.<init>(OfflineDataService.java:23)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at java.lang.Class.newInstanceImpl(Native Method)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at java.lang.Class.newInstance(Class.java:1409)
    10-11 11:45:30.774: E/AndroidRuntime(396):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
    10-11 11:45:30.774: E/AndroidRuntime(396):  ... 10 more

    </code>
    <p><b>Intent Service</b></p>

    <code><pre>
    package com.remote.synchronizer.haris;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.NameValuePair;

import android.app.AlertDialog;
import android.app.IntentService;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class OfflineDataService extends IntentService {

    boolean wifi,edge;
    private Timer timer= new Timer();
    SQLiteDatabase db;
    String un,shop,city,date,order,InsertQuery;
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

    public OfflineDataService() {
        super("OfflineDataService");
    }

    /*@Override   
    public void onCreate() {

        super.onCreate();
         Log.e("Service Example", "Service Started.. ");

        db= openOrCreateDatabase("Product", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Order (UserName VARCHAR(10), Shop VARCHAR(15), City VARCHAR(15), Date VARCHAR(10), Order VARCHAR(50));");

    }   */

    @Override
    protected void onHandleIntent(Intent intent) {

        Bundle bundle=intent.getExtras();
        un=bundle.getString("un");
        shop=bundle.getString("shop");
        city=bundle.getString("city");
        date=bundle.getString("date");
        order=bundle.getString("order");

        /*InsertQuery="INSERT INTO Order VALUES ('"+ un + "','"+ shop + "','" + city + "','" + date + "','" + order + "');";
        db.execSQL(InsertQuery);*/

        /*timer.scheduleAtFixedRate(new TimerTask(){

            @Override
            public void run() {

                //Checking network connectivity
                   wifi=NetworkInfo.Wifi(OfflineDataService.this);
                   edge=NetworkInfo.EDGE(OfflineDataService.this);

                       if(wifi==true||edge==true)
                       {
                           DataSender();
                       }
                       else
                       {

                           dlgAlert.setMessage("Testing");
                           dlgAlert.show();
                           //  Toast toast=Toast.makeText(getApplicationContext(), "Testing", toast.LENGTH_LONG).show();
                       }
            }

        }, 1000, 5000);*/


    }

    /*@Override
    public void onDestroy() {

        super.onDestroy();
        Log.e("Service Example", "Service Destroyed.. ");

        //notifications
    }

    private void DataSender()
    {
        timer.cancel();

    }*/

}

</code></pre>

<p><b>Calling Method</b></p>

<code><pre>

 Intent serviceIntent= new Intent(this, OfflineDataService.class);
               Bundle bundle= new Bundle();
               bundle.putString("un", msg);
               bundle.putString("shop", shop.getText().toString());
               bundle.putString("city", city.getText().toString());
               bundle.putString("date", reportDate);
               bundle.putString("order", order.getText().toString());

               serviceIntent.putExtras(bundle);
               startService(serviceIntent);
</code></pre>

<p><b>AndroidManifest Declaration</b></p>

<code><pre>
android:enabled="true" 
android:name=".OfflineDataService"   
</code></pre>

<p>what is wrong in my code?</p>
<h3><b>Thanks in advance</b></h3>
4

1 回答 1

3

1/您不能使用服务中的警报对话框。

2/ even in an Activity, you cannot use it for a Context before onCreate() (that is, you cannot use it in the instanciation process - not in the contstructor, and not in the member declaration)

于 2012-10-11T07:23:26.477 回答