如何解决java.lang.RuntimeException:无法启动活动?

如何解决java.lang.RuntimeException:无法启动活动?,java,android,android-activity,Java,Android,Android Activity,我正在制作一个具有登录或登录按钮的应用程序,登录按钮工作正常,但注册按钮给我一个致命错误,使AVD停止运行该应用程序,这是注册活动的代码 @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.signup_activity); // Set up the signup form.

我正在制作一个具有登录或登录按钮的应用程序,登录按钮工作正常,但注册按钮给我一个致命错误,使AVD停止运行该应用程序,这是注册活动的代码

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

    // Set up the signup form.
    usernameView = (EditText) findViewById(R.id.username);
    passwordView = (EditText) findViewById(R.id.password);
    passwordAgainView = (EditText) findViewById(R.id.passwordAgain);

    // Set up the submit button click handler
    findViewById(R.id.action_button).setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            // Validate the sign up data
            boolean validationError = false;
            StringBuilder validationErrorMessage =
                    new StringBuilder(getResources().getString(R.string.error_intro));
            if (isEmpty(usernameView)) {
                validationError = true;
                validationErrorMessage.append(getResources().getString(R.string.error_blank_username));
            }
            if (isEmpty(passwordView)) {
                if (validationError) {
                    validationErrorMessage.append(getResources().getString(R.string.error_join));
                }
                validationError = true;
                validationErrorMessage.append(getResources().getString(R.string.error_blank_password));
            }
            if (!isMatching(passwordView, passwordAgainView)) {
                if (validationError) {
                    validationErrorMessage.append(getResources().getString(R.string.error_join));
                }
                validationError = true;
                validationErrorMessage.append(getResources().getString(
                        R.string.error_mismatched_passwords));
            }
            validationErrorMessage.append(getResources().getString(R.string.error_end));

            // If there is a validation error, display the error
            if (validationError) {
                Toast.makeText(SignUpActivity.this, validationErrorMessage.toString(), Toast.LENGTH_LONG)
                        .show();
                return;
            }

            // Set up a progress dialog
            final ProgressDialog dlg = new ProgressDialog(SignUpActivity.this);
            dlg.setTitle("Please wait.");
            dlg.setMessage("Signing up.  Please wait.");
            dlg.show();

            // Set up a new Parse user
            ParseUser user = new ParseUser();
            user.setUsername(usernameView.getText().toString());
            user.setPassword(passwordView.getText().toString());
            // Call the Parse signup method
            user.signUpInBackground(new SignUpCallback() {

                @Override
                public void done(ParseException e) {
                    dlg.dismiss();
                    if (e != null) {
                        // Show the error message
                        Toast.makeText(SignUpActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    } else {
                        // Start an intent for the dispatch activity
                        Intent intent = new Intent(SignUpActivity.this, DispatchActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                }
            });
        }
    });
}

private boolean isEmpty(EditText etText) {
    if (etText.getText().toString().trim().length() > 0) {
        return false;
    } else {
        return true;
    }
}

private boolean isMatching(EditText etText1, EditText etText2) {
    if (etText1.getText().toString().equals(etText2.getText().toString())) {
        return true;
    } else {
        return false;
    }
}
这是舱单

<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"
    android:name=".ParseStarter">
    <activity
        android:screenOrientation="portrait"
        android:name=".MainActivity"
        android:label="@string/app_name" >

    </activity>

    <activity android:name=".SignUpOrLogInActivity"

        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=".DispatchActivity"/>
    <activity android:name=".LoginActivity" />
    <activity android:name=".SignUpActivity" />


    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>


</application>

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
下面是具有调用注册活动的按钮的类的代码

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

    // Log in button click handler
    ((Button) findViewById(R.id.login)).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Starts an intent of the log in activity
            startActivity(new Intent(SignUpOrLogInActivity.this, LoginActivity.class));
        }
    });

    // Sign up button click handler
    ((Button) findViewById(R.id.signup)).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Starts an intent for the sign up activity
            startActivity(new Intent(SignUpOrLogInActivity.this, SignUpActivity.class));
        }
    });
}
}

这是注册的布局图

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#67AAE4">


    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/prompt_username"
        android:inputType="textEmailAddress"
        android:maxLines="1"
        android:singleLine="true" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/prompt_password"
        android:inputType="textPassword"
        android:maxLines="1"
        android:singleLine="true" />

    <EditText
        android:id="@+id/passwordAgain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/prompt_password_again"
        android:imeActionLabel="@string/action_sign_in_short"
        android:imeOptions="actionUnspecified"
        android:inputType="textPassword"
        android:maxLines="1"
        android:singleLine="true" />



</LinearLayout>

这是“注册”按钮出现的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#67AAE4">

<Button
    android:id="@+id/signup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sign Up"/>

<Button
    android:id="@+id/login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Login"/>


<com.facebook.widget.LoginButton
    android:id="@+id/authButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    />

非常感谢您阅读:)

您的
signup\u activity.xml
布局似乎没有id为
action\u按钮的视图,并且
findViewById()
返回空值


似乎您的
signup\u activity.xml
布局没有id为
action\u按钮的视图,并且
findViewById()
返回null。

未找到要将onClickListener添加到的视图,
r.id.action\u按钮
,因此出现NullPointerException

因此,请确保它位于当前视图中,
signup\u activity.xml

<Button
android:id="@+id/action_button"
android:onClick="onSignUpClick"
...
/>
更多信息:

<> >为了使您的“OnCuto”-方法不太笨重,并且避免再次发生这种情况,请考虑在LayOutFutton中使用“<代码> Android:OnCaseS/<代码> > <代码> SimuluPix.Actudio.XML<代码> >

<Button
android:id="@+id/action_button"
android:onClick="onSignUpClick"
...
/>

找不到要将onClickListener添加到的视图,
r.id.action\u按钮
,因此出现NullPointerException

因此,请确保它位于当前视图中,
signup\u activity.xml

<Button
android:id="@+id/action_button"
android:onClick="onSignUpClick"
...
/>
更多信息:

<> >为了使您的“OnCuto”-方法不太笨重,并且避免再次发生这种情况,请考虑在LayOutFutton中使用“<代码> Android:OnCaseS/<代码> > <代码> SimuluPix.Actudio.XML<代码> >

<Button
android:id="@+id/action_button"
android:onClick="onSignUpClick"
...
/>


您好,请附上错误的
logcat
。添加堆栈跟踪。否则,搜索代码中的错误将需要一些时间。好的,但我这里有点新,我不确定如何正确地将日志放在帖子上,我应该粘贴它吗?仅此而已?将它插入代码块中。与您在清单和活动类中所做的相同。您是否碰巧使用了片段?您好,请附上错误的
logcat
。添加堆栈跟踪。否则,搜索代码中的错误将需要一些时间。好的,但我这里有点新,我不确定如何正确地将日志放在帖子上,我应该粘贴它吗?仅此而已?将它插入代码块中。和你在清单和活动类中做的一样。你有机会使用片段吗?很少见,因为它确实有按钮,让我把xml代码放在问题上。那么包含
action\u按钮的布局在哪里呢,多大的一个转储错误,非常感谢。它很罕见,因为它确实有按钮,让我把xml代码放在问题上。那么,包含
操作按钮的布局在哪里?发布R.layout.signup\u活动的xml-这就是它查找
操作按钮的地方你是对的,多大的转储错误,非常感谢。看起来他已经在使用onClick属性了,但是出于某种原因,单击listener set为null,而不是相应的组件(活动、片段等),xml中目前没有onClick属性的迹象。您实际上是在视图上添加onclick侦听器。但是如果没有xml,很难说。你能添加注册活动的xml吗?你能在其中找到id为
action\u按钮的视图吗?;)我们的两个答案都支持这个解决方案——在布局中没有id
action\u按钮的视图(按钮或任何东西)。您的程序在onCreate()中崩溃,其中有“findViewById(R.id.action_按钮)”—因为没有—即NullPointerException—看起来他已经使用了onClick属性,但由于某些原因,请单击listener set(侦听器设置)为null,而不是相应的组件(活动、片段等)。xml中当前没有onClick属性的迹象。您实际上是在视图上添加onclick侦听器。但是如果没有xml,很难说。你能添加注册活动的xml吗?你能在其中找到id为
action\u按钮的视图吗?;)我们的两个答案都支持这个解决方案——在布局中没有id
action\u按钮的视图(按钮或任何东西)。您的程序在onCreate()中崩溃,其中有“findViewById(R.id.action_按钮)”—因为没有—即NullPointerException