应用程序崩溃:致命异常:主要

时间:2015-03-11 06:24:39

标签: android-studio onclick crash onclicklistener

按钮点击时应用崩溃。这是日志。

enter image description here

专门处理尝试将用户存储到Parse.com作为我的后端。我有一个名为RegisterActivity的java类与相应的xml文件,导致我认为它可能是一个空指针异常,但我测试了,这似乎不是问题。以下是我认为涉及该问题的3个类。

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;

public class RegisterActivity extends Activity {

protected EditText mUsername;
protected EditText mUserEmail;
protected EditText mUserPassword;
protected Button mRegisterButton;

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

    //initialize
    mUsername = (EditText)findViewById(R.id.usernameRegisterEditText);
    mUserEmail= (EditText)findViewById(R.id.emailRegisterEditText);
    mUserPassword= (EditText)findViewById(R.id.passwordRegisterEditText);
    mRegisterButton = (Button)findViewById(registerButton);

    //listen to register button click
    mRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //get the username,password, and email, then convert to string
            String username = mUsername.getText().toString().trim();
            String email = mUserEmail.getText().toString().trim();
            String password = mUserPassword.getText().toString().trim();

            //store user and parse
            ParseUser user = new ParseUser();
            user.setUsername(username);
            user.setPassword(email);
            user.setEmail(password);

            user.signUpInBackground(new SignUpCallback() {
                @Override
                public void done(ParseException e) {
                    if(e == null){
                        //user signed up successfully
                        Toast.makeText(RegisterActivity.this, "Welcome!", Toast.LENGTH_LONG).show();

                        //take user to homepage

                    }else{
                        //error signing up user
                    }
                }
            });
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_register, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这是相应的activity_register.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.parse.inclassmode.RegisterActivity">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:hint="username"
    android:ems="10"
    android:id="@+id/usernameRegisterEditText"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="142dp" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:hint="email"
    android:ems="10"
    android:id="@+id/emailRegisterEditText"
    android:layout_below="@+id/usernameRegisterEditText"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:hint="password"
    android:ems="10"
    android:id="@+id/passwordRegisterEditText"
    android:layout_below="@+id/emailRegisterEditText"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="sign up"
    android:id="@+id/registerButton"
    android:layout_marginTop="42dp"
    android:layout_below="@+id/passwordRegisterEditText"
    android:layout_alignParentStart="true" />
</RelativeLayout>

MainActivity.java类,其中包含我的Parse.com密钥

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.parse.Parse;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Enable Local Datastore, ID's hidden
    Parse.initialize(this, "id", "id");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

最后我的android清单启动了注册活动,然后将用户带到主屏幕。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parse.inclassmode" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".RegisterActivity"
        android:label="@string/title_activity_register" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="InClassMode" >
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

想出来! 对于那些可能遇到此问题的人,创建一个非活动的新类,从那里初始化Parse。