gpt4 book ai didi

java - 更改代码后诊断 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 11:42:19 25 4
gpt4 key购买 nike

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

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

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

activity_login2.xml 文件是:

<LinearLayout    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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.redux.kumardivyarajat.attendance.LoginActivity"
android:weightSum="1"
android:id="@+id/activity_login2">

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

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

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

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


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

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

</LinearLayout>
</ScrollView>

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

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

</LinearLayout>

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

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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2239)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
at com.android.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 android.app.Activity.performCreate(Activity.java:5249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)

           

我了解 NullPointerException 的常见原因,但我无法确定它在我的代码中是如何发生的。我该如何解决这个问题?

最佳答案

事实证明,由于我的应用的某些元素不适用于 API 级别 14 及以下版本,因此单独的 activity_login2.xmlactivity_login2.xml(v14) 文件已已自动生成。

出现 NullPointerException 是因为第二个 activity_login2.xml(v14) 文件中未定义新的注册按钮。我已经更新了该文件,现在应用程序运行良好。

关于java - 更改代码后诊断 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com