2012-10-11 62 views
1

我的意图服务未启动。这是一个错误。我是Android新手。我正在安静的web api上工作。我在Android设备上使用服务。我创建了一个同步模块,其中如果没有覆盖区域的应用程序将数据存储在数据库中,并在每60秒检查一次网络。Android IntentService:无法启动

当它上线时,它会在服务器上传数据。我希望这个过程在后台运行。为此,我使用了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> 

回答

3

1 /您不能使用服务中的alertdialog。

2 /即使在活动,之前的onCreate()(即,不能在instanciation过程中使用它 - 在成员声明没有在contstructor,而不是),则可以不使用它的上下文

+0

谢谢。我的问题解决了。 – Haris