0

这段代码的作用是将用户注册到服务器以供 gcm 和我的服务器添加到 mysql 数据库中。它还写入用户偏好。该应用程序在 gcm 服务器和我的服务器上完成创建用户后意外停止。请帮帮我?

package com.package.name;
import static com.package.name.CommonUtilities.SENDER_ID;
import static com.package.name.CommonUtilities.SERVER_URL;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegisterActivity extends Activity {
// alert dialog manager
AlertDialogManager alert = new AlertDialogManager();

// Internet detector
ConnectionDetector cd;

// UI elements
EditText txtName;
EditText txtEmail;

// Register button
Button btnRegister;

// Session Manager Class
SessionManager session;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    // Session Manager
    session = new SessionManager(getApplicationContext()); 

    cd = new ConnectionDetector(getApplicationContext());

    session.checkLogin();

    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(RegisterActivity.this,
                "Internet Connection Error",
                "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }

    // Check if GCM configuration is set
    if (SERVER_URL == null || SENDER_ID == null || SERVER_URL.length() == 0
            || SENDER_ID.length() == 0) {
        // GCM sernder id / server url is missing
        alert.showAlertDialog(RegisterActivity.this, "Configuration Error!",
                "Please set your Server URL and GCM Sender ID", false);
        // stop executing code by return
         return;
    }

    txtName = (EditText) findViewById(R.id.txtName);
    txtEmail = (EditText) findViewById(R.id.txtEmail);
    btnRegister = (Button) findViewById(R.id.btnRegister);

    /*
     * Click event on Register button
     * */
    btnRegister.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Read EditText dat
            String name = txtName.getText().toString();
            String email = txtEmail.getText().toString();


            // Check if user filled the form
            if(name.trim().length() > 0 && email.trim().length() > 0){
                // Launch Main Activity
                Intent i = new Intent(getApplicationContext(), MainActivity.class);


                // Registering user on our server                   
                // Sending registraiton details to MainActivity
                i.putExtra("name", name);
                i.putExtra("email", email);
                session.registerEntry();

                SharedPreferences pref = getApplicationContext().getSharedPreferences("A4APrefs", 0); // 0 - for private mode
                Editor editor = pref.edit();

                Random r = new Random();
                int pairId = r.nextInt(999999-100000) + 100000;


                editor.putInt("conId", pairId); // Storing integer

                editor.commit(); // commit changes
                startActivity(i);
                finish();
            }else{
                // user doen't filled that data
                // ask him to fill the form
                alert.showAlertDialog(RegisterActivity.this, "Registration Error!", "Please enter your details", false);
            }
        }
    });
}

}

为什么此活动在运行一次后关闭?我对android真的很陌生,对它了解不多。

错误 LogCat 如下。

06-19 23:26:03.007: E/GCMRegistrar(29350): internal error: retry receiver class not set yet
06-19 23:26:03.071: E/URL(29350): > http://some.com/gcm/register.php
06-19 23:26:04.049: E/AndroidRuntime(29350): FATAL EXCEPTION: IntentService[GCMIntentService-950252240713-2]
06-19 23:26:04.049: E/AndroidRuntime(29350): java.lang.NullPointerException
06-19 23:26:04.049: E/AndroidRuntime(29350):    at com.package.name.GCMIntentService.generateNotification(GCMIntentService.java:90)
06-19 23:26:04.049: E/AndroidRuntime(29350):    at com.package.name.GCMIntentService.onMessage(GCMIntentService.java:48)
06-19 23:26:04.049: E/AndroidRuntime(29350):    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
06-19 23:26:04.049: E/AndroidRuntime(29350):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
06-19 23:26:04.049: E/AndroidRuntime(29350):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 23:26:04.049: E/AndroidRuntime(29350):    at android.os.Looper.loop(Looper.java:154)
06-19 23:26:04.049: E/AndroidRuntime(29350):    at android.os.HandlerThread.run(HandlerThread.java:65)
06-19 23:26:05.056: E/UnRegister Receiver Error(29350): > Receiver not registered: com.google.android.gcm.GCMBroadcastReceiver@413d8ab8

根据 LogCat,这一行是 GCMIntent 正在创建错误:

String[] separated = mmessage.split("::");
    String epurl = separated[2];
    String message = separated[1];
    String ntitle = separated[0];

我认为只有在这些字符串中包含“::”时才能通过转换这些字符串来解决解决方案,你能帮帮我吗?

4

1 回答 1

1

如果您在NullPointerException指定的行上得到 a,则唯一可能的解释mmessage是 null。

如果问题是mmessage字符串没有足够的"::"分隔符,则应该是异常,ArrayIndexOutOfBoundsException并且它应该在其他行之一。

简单的解决方案是检查是否为mmessage空,如果是则不执行该代码序列。但是,我希望它为 null 的事实表明您的代码中其他地方存在更大的问题。

于 2013-06-19T22:35:10.240 回答