当我点击登录按钮时,应用程序崩溃

时间:2015-07-10 09:25:38

标签: java android xml android-intent android-activity

这是我的注册活动&主要活动及其xml

            public class SignupActivity extends AppCompatActivity {
                protected EditText mUsername;
                protected EditText mPassword;
                protected EditText mEmail;
                protected Button nbutton;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_signup);
                mUsername = (EditText) findViewById(R.id.usernamefield);
                mPassword = (EditText) findViewById(R.id.passwordtextfield);
                mEmail = (EditText) findViewById(R.id.emailtextfield);
                nbutton = (Button) findViewById(R.id.signbutton);
                nbutton.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View view){
                        // Retrieve the text entered from the EditText
                        String usernametxt = mUsername.getText().toString();
                        String password = mPassword.getText().toString();
                        String email= mEmail.getText().toString();
                        // Force user to fill up the form
                        if (usernametxt.equals("") && password.equals("")) {
                            Toast.makeText(getApplicationContext(),
                                    "Please complete the sign up form",
                                    Toast.LENGTH_LONG).show();

                        } else {
                            // Save new user data into Parse.com Data Storage

                            ParseUser newUser =  new ParseUser();
                            newUser.setUsername(usernametxt);
                            newUser.setPassword(password);
                            newUser.setEmail(email);
                            newUser.signUpInBackground(new SignUpCallback() {
                                @Override
                                public void done(ParseException e) {
                                    //SUCESS
                                    if (e!= null){

                                        AlertDialog.Builder builder=new AlertDialog.Builder(SignupActivity.this);
                                        builder.setMessage(e.getMessage()).setTitle(R.string.signup_error_title).setPositiveButton(android.R.string.ok, null);
                                        AlertDialog dialog = builder.create();
                                        dialog.show();
                                    }
                                    else {


                                        Intent intent= new Intent(SignupActivity.this,MainActivity.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                        startActivity(intent);

                                    }
                                    }

                            });
                        }

                    }
                });

            }

假设从用户那里获取信息并从signupactivity转到主要活动。当应用程序从用户进入并按下登录时,应用程序崩溃...

这是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.josephvarkey996gmail.test1.SignupActivity" >

        <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/usernamefield"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="89dp"
            android:hint="@string/username_hint"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/passwordtextfield"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/usernamefield"
            android:hint="@string/password_hint"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/emailtextfield"
            android:hint="@string/email_hint"
            android:layout_below="@+id/passwordtextfield"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/signup_hint"
            android:id="@+id/signbutton"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

    here is the main activity code

        package com.josephvarkey996gmail.test1;

        import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.util.Log;
        import android.view.Menu;
        import android.view.MenuItem;

        import com.parse.Parse;
        import com.parse.ParseAnalytics;
        import com.parse.ParseUser;

        public class MainActivity extends AppCompatActivity {

            public static final String TAG = MainActivity.class.getSimpleName();

            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Parse.enableLocalDatastore(this);
                Parse.initialize(this, "XFigOgliUYKi9h5RanLfLkuKU14AG2f2NFQXADKI", "sMQBPkhZV5b74MEGpP3PdQ6eePWo5Y9O8lRcQvBP");
                ParseAnalytics.trackAppOpenedInBackground(getIntent());


                ParseUser currentUser = ParseUser.getCurrentUser();
                if(currentUser==null) {
                    navigatetologin();
                }
                if(currentUser!=null)
                {
                    Log.i(TAG ,currentUser.getUsername());
                }
                // Enable Local Datastore.


            }

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



            private void navigatetologin() {
                Intent intent = new Intent(this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        }

mainactivity 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=".MainActivity" >

            <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>

android manifest

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

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

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".SignupActivity"
                android:label="@string/title_activity_signup" >
            </activity>
            <activity
                android:name=".LoginActivity"
                    android:label="@string/title_activity_login" >
                </activity>
            </application>
        </manifest>

1 个答案:

答案 0 :(得分:0)

在你发布logcat之前我们无法帮助你。由于我没有足够的声誉来发表评论,我正在回答这个问题。

我看到有人收到此错误的最常见原因是: 他们从另一个类

复制-paste这一行
R.id.usernamefield

所以它会尝试运行其他类的用户区域,这里不存在(注意:eclipse会自动导入,所以他们没有得到编译错误)。因此,要解决您的错误,您应该删除导入并写入正确的R.id.xyz

但这只是在黑暗中拍摄,直到我们看到你的logcat。

相关问题