有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

未调用java onCreate()和onStarCommand()方法

我有一个使用以下代码的服务:

package com.ritvik.weaselyclock;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import 安卓.app.IntentService;
import 安卓.app.NotificationManager;
import 安卓.app.PendingIntent;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.os.Handler;
import 安卓.support.v4.app.NotificationCompat;
import 安卓.support.v4.app.TaskStackBuilder;
import 安卓.util.Log;
import 安卓.widget.Toast;

import com.google.安卓.gms.gcm.GoogleCloudMessaging;
import com.ritvik.utils.JSONUtils;

public class GcmIntentService extends IntentService {
    public final static String CLOCK_ANG = "com.tutecentral.navigationdrawer.CLOCKANG";
    public static final int NOTIFICATION_ID = 1;
    NotificationCompat.Builder builder;
    Handler mHandler;
    static final String TAG = "Receive Message Activity";


    public GcmIntentService() {
        super("44937255078");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mHandler = new Handler();
        return startId; 
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // Post notification of received message.
                Log.i(TAG, "Received: " + extras.toString());
                handleMessage(extras.getString("message"));
            } 
        }
    }
    public void handleMessage(final String msg) {
        mHandler.post(new Runnable() {            
            @Override
            public void run() {
                JSONObject JSON = null;
                String type = "";
                try {
                    JSON = new JSONObject(msg);
                    type = JSON.getString("type");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if (type.equals("ru_101")) {
                    startActivity(new Intent(GcmIntentService.this, MainActivity.class));
                    Toast.makeText(GcmIntentService.this, "Registration successful", Toast.LENGTH_LONG).show();
                }

                else if (type.equals("ru_002")) {
                    Toast.makeText(GcmIntentService.this, "Registration successfully updated", Toast.LENGTH_LONG).show();
                }

                else if (type.equals("ru_301")) {
                    NotificationCompat.Builder mBuilder =
                            new NotificationCompat.Builder(GcmIntentService.this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
                    // Creates an explicit intent for an Activity in your app
                    Intent resultIntent = new Intent(GcmIntentService.this, ResultActivity.class);
                    try {
                        resultIntent.putExtra("com.ritvik.weaselyclock.phone", JSON.getString("phone_number"));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // The stack builder object will contain an artificial back stack for the
                    // started Activity.
                    // This ensures that navigating backward from the Activity leads out of
                    // your application to the Home screen.
                    TaskStackBuilder stackBuilder = TaskStackBuilder.create(GcmIntentService.this);
                    // Adds the back stack for the Intent (but not the Intent itself)
                    stackBuilder.addParentStack(ResultActivity.class);
                    // Adds the Intent that starts the Activity to the top of the stack
                    stackBuilder.addNextIntent(resultIntent);
                    PendingIntent resultPendingIntent =
                            stackBuilder.getPendingIntent(
                                    0,
                                    PendingIntent.FLAG_UPDATE_CURRENT
                                    );
                    mBuilder.addAction(R.drawable.ic_action_good, "Accept", resultPendingIntent);
                    mBuilder.addAction(R.drawable.ic_action_discard, "Reject", resultPendingIntent);
                    NotificationManager mNotificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    int mId = 001;
                    // mId allows you to update the notification later on.
                    mNotificationManager.notify(mId, mBuilder.build());
                }
                else if (  type.equals("ru_101") || type.equals("ru_102") 
                        || type.equals("ru_103") || type.equals("ru_201") 
                        || type.equals("ru_202") || type.equals("ru_203") 
                        || type.equals("ru_204") || type.equals("ru_205") 
                        || type.equals("ru_206") || type.equals("ru_207") 
                        || type.equals("ru_208") || type.equals("ru_209") 
                        || type.equals("ru_210") || type.equals("ru_211") 
                        || type.equals("jr_201") || type.equals("jr_202")
                        || type.equals("jr_203") || type.equals("jr_204")
                        || type.equals("jr_205") || type.equals("jr_206")
                        || type.equals("jr_207") || type.equals("jr_208")
                        || type.equals("jr_209") || type.equals("id_201")
                        || type.equals("id_202") || type.equals("id_203")
                        || type.equals("id_204") || type.equals("id_205")
                        || type.equals("id_206") || type.equals("id_207")
                        || type.equals("id_208") || type.equals("sm_201")
                        || type.equals("sm_202") || type.equals("sm_203")
                        || type.equals("sm_204") || type.equals("sm_205")
                        || type.equals("sm_206") || type.equals("sm_207")
                        || type.equals("sm_208") || type.equals("sm_209")
                        || type.equals("sm_210")) {
                    try {
                        Log.e("Server error:", JSON.getString("error"));
                        Toast.makeText(GcmIntentService.this, "Server error: " + JSON.getString("error"), Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                else if (type.equals("jr_101") || type.equals("jr_102")) {
                    try {
                        Toast.makeText(GcmIntentService.this, JSON.getString("action"), Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                else if (type.equals("jr_301") || type.equals("jr_302")) {
                    try {
                        Toast.makeText(GcmIntentService.this, "You have been " + JSON.getString("action") + "by group" + JSON.getString("group") , Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                else if (type.equals("id_101")) {
                    try {
                        JSONArray members = JSON.getJSONArray("members");
                        JSONUtils.writeJSON("com.ritvik.weaselyclock.MEMBERS", GcmIntentService.this, members, "members");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}

由于某些原因,根据调试器,onStartCommand()方法从未被调用。我曾尝试使用onCreate(),但也没有被调用。然后应用程序崩溃,出现以下LogCat输出:

04-28 08:25:35.699: E/AndroidRuntime(11113): FATAL EXCEPTION: Thread-775
04-28 08:25:35.699: E/AndroidRuntime(11113): java.lang.NullPointerException
04-28 08:25:35.699: E/AndroidRuntime(11113):    at com.ritvik.weaselyclock.GcmIntentService.handleMessage(GcmIntentService.java:69)
04-28 08:25:35.699: E/AndroidRuntime(11113):    at com.ritvik.utils.HTTPUtils$1.run(HTTPUtils.java:48)
04-28 08:25:35.699: E/AndroidRuntime(11113):    at java.lang.Thread.run(Thread.java:838)

编辑:问题不是没有调用方法,而是处理程序没有正确初始化

感谢您的帮助。谢谢


共 (0) 个答案