从服务调用方法时出现NullPointerException

时间:2014-04-12 16:33:02

标签: android

我的目标是创建一个列表,当我更改表格中的一行时立即刷新我在Asynctask中使用相同的代码并且它完美地运行,问题似乎来自服务。

以下是我如何调用我的服务方法

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_liste_offre);
    new ConsultationOffreAsyncTask().execute();
    Intent intent = new Intent(this, ServiceConsultation.class);
    bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
    try{

    }catch(NullPointerException e){
        e.printStackTrace();
    }
    listview = (ListView) this.findViewById(R.id.listeoffre);

这是相应的方法GetOffres()

        public void GetOffres() {

        Runnable runnable = new Runnable() {
            public void run() {

                try {
                    String myurl = "http://10.0.2.2/PixelsTrade/consultation_offre.php";
                    List<NameValuePair> params = new ArrayList<NameValuePair>();

                    JSONParser json = new JSONParser();

                    JSONObject result = json.makeHttpRequest(myurl, "POST",
                            params);

                    offre.open();

                    JSONArray productObjectArray = result
                            .getJSONArray("offre");
                    for (int j = 0; j < productObjectArray.length(); j++) {
                        JSONObject productObject = productObjectArray
                                .getJSONObject(j);

                        int b = 0;
                        if (b != productObject.getInt("CodeOffre")) {
                            b = productObject.getInt("CodeOffre");
                            if (offre.chekOffre(b) == false) {

                                offre.insertOffre(productObject
                                        .getInt("CodeOffre"), productObject
                                        .getString("NomOffre"),
                                        productObject.getInt("CodeUser"),
                                        productObject
                                                .getString("DateDebut"),
                                        productObject.getString("DateFin"),
                                        productObject
                                                .getString("Description"),
                                        productObject.getString("maj"));
                                Log.i("insert", "insert rows");

                            } else if (offre.chekOffre(b) == true) {
                                offre.updateOffre(productObject
                                        .getInt("CodeOffre"), productObject
                                        .getString("NomOffre"),
                                        productObject.getInt("CodeUser"),
                                        productObject
                                                .getString("DateDebut"),
                                        productObject.getString("DateFin"),
                                        productObject
                                                .getString("Description"),
                                        productObject.getString("maj"));
                                Log.i("update", "update rows");

                            }

                        }

                    }

                    offre.close();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };
        Thread mythread = new Thread(runnable);
        mythread.start();

        offre = new OffreBDD(ListeOffre.this);
        offre.open();// c est pour autoriser l acces de lire de la base
        // get all produits
        list = (ArrayList<Offre>) offre.getAllOffres();

        myAdapter = new AdapterOffre(ListeOffre.this, list);
        listview.setAdapter(myAdapter);
        offre.close();


    }

我在调用myService.getOffre()的行中得到NullPointerException 有人可以告诉错误在哪里

这是您提出的logcat

04-12 17:22:25.972: W/System.err(7990): java.lang.NullPointerException
04-12 17:22:25.982: W/System.err(7990):     at com.Consultation.applicationpfe.ListeOffre.onCreate(ListeOffre.java:59)
04-12 17:22:25.982: W/System.err(7990):     at android.app.Activity.performCreate(Activity.java:5104)
04-12 17:22:25.982: W/System.err(7990):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-12 17:22:25.982: W/System.err(7990):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-12 17:22:25.982: W/System.err(7990):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
04-12 17:22:25.982: W/System.err(7990):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
04-12 17:22:25.982: W/System.err(7990):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
04-12 17:22:25.982: W/System.err(7990):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
04-12 17:22:25.992: W/System.err(7990):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
04-12 17:22:25.992: W/System.err(7990):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
04-12 17:22:25.992: W/System.err(7990):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
04-12 17:22:25.992: W/System.err(7990):     at android.view.View.performClick(View.java:4204)
04-12 17:22:25.992: W/System.err(7990):     at android.view.View$PerformClick.run(View.java:17355)
04-12 17:22:26.002: W/System.err(7990):     at android.os.Handler.handleCallback(Handler.java:725)
04-12 17:22:26.002: W/System.err(7990):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-12 17:22:26.002: W/System.err(7990):     at android.os.Looper.loop(Looper.java:137)
04-12 17:22:26.002: W/System.err(7990):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-12 17:22:26.002: W/System.err(7990):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 17:22:26.012: W/System.err(7990):     at java.lang.reflect.Method.invoke(Method.java:511)
04-12 17:22:26.012: W/System.err(7990):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-12 17:22:26.021: W/System.err(7990):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-12 17:22:26.021: W/System.err(7990):     at dalvik.system.NativeStart.main(Native Method)

以下是我对该服务进行绑定的方式。

eprivate ServiceConnection myConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
        MyBinder binder = (MyBinder) service;
        myService = binder.getService();
        isBound = true;
        myService.GetOffres();
        Log.i("service", "service connected");
    }

    public void onServiceDisconnected(ComponentName arg0) {
        isBound = false;
    }

    void doBindService() {
        // Establish a connection with the service.  We use an explicit
        // class name because we want a specific service implementation that
        // we know will be running in our own process (and thus won't be
        // supporting component replacement by other applications).
        bindService(new Intent(ListeOffre.this, 
                ServiceConsultation.class), myConnection, Context.BIND_AUTO_CREATE);
        isBound = true;
    }

    void doUnbindService() {
        if (isBound) {
            // Detach our existing connection.
            unbindService(myConnection);
            isBound = false;
        }
    }

2 个答案:

答案 0 :(得分:0)

您没有显示处理服务绑定回调的方式/位置。你应该有这样的东西(主要来自Bound Services page

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection myConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};

}

答案 1 :(得分:0)

我找到了问题的解决方案,似乎我在清单中使用错误的名称声明了服务。 谢谢你的提议。