简体   繁体   English

为什么此活动意外关闭了我的Android?

[英]Why does this activity unexpectedly closes my android?

What this code does is that it registers the user to the server for gcm and to my server to add in the mysql database. 此代码的作用是将用户注册到gcm的服务器,并注册到我的服务器以添加到mysql数据库中。 It also writes into user preference. 它还写入用户首选项。 The app unexpectedly stops after it completes creating the user on the gcm server as well as my server. 在gcm服务器和我的服务器上完成创建用户后,该应用程序意外停止。 Please help me out? 请帮帮我吗?

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);
            }
        }
    });
}

}

Why does this activity close after running itself once? 为什么该活动运行一次后会关闭? I am really new to android and don't know much about it. 我对android真的很陌生,对此了解不多。

Error LogCat is as Follows. 错误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

And as per LogCat this line is GCMIntent is creating the error: 根据LogCat,这行是GCMIntent在创建错误:

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

I think the solution will be solved by converting these strings only if they contain the "::" in them, can you help me out? 我认为只有在这些字符串中包含“ ::”的情况下,才能通过转换这些字符串来解决该问题,您能帮帮我吗?

If you're getting a NullPointerException on the line you specified, the only possible explanation is that mmessage is null. 如果在指定的行上收到NullPointerException ,则唯一可能的解释是mmessage为null。

If the problem was that the mmessage string didn't have enough "::" separators, the exception should have been ArrayIndexOutOfBoundsException and it would have been on one of the other lines. 如果问题是mmessage字符串没有足够的"::"分隔符,则该异常应为ArrayIndexOutOfBoundsException并且该异常应在其他一行中。

The simple solution would be to check for mmessage being null and not execute that sequence of code if it is. 一个简单的解决方案是检查mmessage是否为空,如果不执行,则不执行该代码序列。 However I expect the fact that it is null is a sign of a bigger problem elsewhere in your code. 但是,我希望它为null的事实表明代码中其他地方存在更大的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM