首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓IntentService:无法启动

安卓IntentService:无法启动
EN

Stack Overflow用户
提问于 2012-10-11 15:16:21
回答 1查看 880关注 0票数 1

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

当它上线时,它会将数据上传到服务器上。我希望这个进程在后台运行。为此,我使用了Intent Service,它在后台运行,并在执行后通知用户。但是服务没有启动,我在网上查看了很多教程,它们都是相同的方法。但是我的坏了。

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-11 15:23:26

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

2/即使在活动中,也不能在onCreate()之前的上下文中使用它(也就是说,不能在实例化过程中使用它--不能在contstructor中使用,也不能在成员声明中使用)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12834163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档