有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在我更改代码后诊断NullPointerException

我正在尝试使用Android Studio创建一个应用程序。我使用了默认的登录活动模板,然后创建了一个新的单独的注册活动。它正在与一个NullPointerException崩溃。我试图将这两项活动联系在一起,如下所示:

物流活动。java:

public class LoginActivity extends Activity implements LoaderCallbacks<Cursor> {

/**
 * A dummy authentication store containing known user names and passwords.
 * TODO: remove after connecting to a real authentication system.
 */


private static final String[] DUMMY_CREDENTIALS = new String[]{
    "foo@example.com:hello", "bar@example.com:world"
};

/**
 * Keep track of the login task to ensure we can cancel it if requested.
 */
private UserLoginTask mAuthTask = null;

// UI references.
private AutoCompleteTextView mEmailView;
private EditText mPasswordView;
private View mProgressView;
private View mLoginFormView;

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

    Button mSignUpTextView;
    mSignUpTextView = (Button)findViewById(R.id.SignUpButton);
    mSignUpTextView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
            startActivity(intent);
        }
    });


    // Set up the login form.
    mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
    populateAutoComplete();

    mPasswordView = (EditText) findViewById(R.id.password);
    mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
            if (id == R.id.login || id == EditorInfo.IME_NULL) {
                attemptLogin();
                return true;
            }
            return false;
        }
    });

    Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
    mEmailSignInButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            attemptLogin();
        }
    });

    mLoginFormView = findViewById(R.id.login_form);
    mProgressView = findViewById(R.id.login_progress);
}

注册活动。java

public class SignUpActivity extends ActionBarActivity {

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


    @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_sign_up, 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);
    }
}

以及活动\u login2。xml文件是:

<LinearLayout    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:gravity="center_horizontal"
安卓:orientation="vertical"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.redux.kumardivyarajat.attendance.LoginActivity"
安卓:weightSum="1"
安卓:id="@+id/activity_login2">

<!-- Login progress -->
<ProgressBar 安卓:id="@+id/login_progress"
    style="?安卓:attr/progressBarStyleLarge"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginBottom="8dp"
    安卓:visibility="gone" />

<ScrollView 安卓:id="@+id/login_form"
    安卓:layout_width="match_parent"
    安卓:layout_height="174dp">

    <LinearLayout 安卓:id="@+id/email_login_form"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:orientation="vertical"
        安卓:weightSum="1"
        安卓:touchscreenBlocksFocus="false">

        <AutoCompleteTextView
            安卓:id="@+id/email"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:hint="@string/prompt_email"
            安卓:inputType="textEmailAddress"
            安卓:maxLines="1"
            安卓:singleLine="true"
            安卓:layout_weight="10.63">
            <requestFocus/>
            </AutoCompleteTextView>


        <EditText 安卓:id="@+id/password"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:hint="@string/prompt_password"
            安卓:imeActionId="@+id/login"
            安卓:imeActionLabel="@string/action_sign_in_short"
            安卓:imeOptions="actionUnspecified"
            安卓:inputType="textPassword"
            安卓:maxLines="1"
            安卓:singleLine="true" />

        <Button 安卓:id="@+id/email_sign_in_button"
            style="?安卓:textAppearanceSmall"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_marginTop="16dp"
            安卓:text="@string/action_sign_in_short"
            安卓:textStyle="bold" />

    </LinearLayout>
</ScrollView>

<EditText
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:id="@+id/SignupText"
    安卓:text="@string/sign_up_from_login"
    安卓:textColor="#ffff0500" />

<Button
    style="?安卓:textAppearanceSmall"
    安卓:id="@+id/SignUpButton"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="16dp"
    安卓:text="Sign Up"
    安卓:textStyle="bold"
    安卓:layout_gravity="center_horizontal" />

</LinearLayout>

在登录活动中添加新的注册按钮之前,该应用程序运行良好,但现在立即崩溃

logcat显示NullPointerException

    04-03 19:29:44.486    4648-4648/com.redux.kumardivyarajat.attendance E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.redux.kumardivyarajat.attendance, PID: 4648
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.redux.kumardivyarajat.attendance/com.redux.kumardivyarajat.attendance.LoginActivity}: java.lang.NullPointerException
        at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
        at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2239)
        at 安卓.app.ActivityThread.access$800(ActivityThread.java:141)
        at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
        at 安卓.os.Handler.dispatchMessage(Handler.java:102)
        at 安卓.os.Looper.loop(Looper.java:136)
        at 安卓.app.ActivityThread.main(ActivityThread.java:5047)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:622)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.redux.kumardivyarajat.attendance.LoginActivity.onCreate(LoginActivity.java:69)
        at 安卓.app.Activity.performCreate(Activity.java:5249)
        at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)

我理解NullPointerException的常见原因,但我很难在代码中确定它是如何可能的。我怎样才能解决这个问题


共 (2) 个答案

  1. # 1 楼答案

    事实证明,由于我的应用程序的某些元素不适用于API级别14及以下的应用程序,因此请将activity_login2分开。xml活动登录2。xml(v14)文件已自动生成

    之所以出现NullPointerException,是因为在第二个活动\u login2中没有定义新的注册按钮。xml(v14)文件。我已经更新了该文件,现在该应用程序运行良好

  2. # 2 楼答案

    请检查最后2个元素-编辑文本

    android:id="@+id/SignupText"
    

    和按钮

    android:id="@+id/SignUpButton"
    

    是否具有有效的父布局

    我看不到为这些UI元素定义的父布局