更改密码活动



我在这个应用程序中遇到了问题,它向我显示"不幸的是,应用程序已停止"

主要活动.class

        package ahmedchtn.stockmanager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    EditText username;
    EditText password;
    Button loginbutton;
    Button bpasschange;
    int counter = 3;
    String username_string  ;
    String password1 = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = (EditText) findViewById(R.id.identifer);
        password = (EditText) findViewById(R.id.password);
        loginbutton = (Button) findViewById(R.id.bLogin);
        bpasschange = (Button) findViewById(R.id.bCh);
        username_string = "admin";
        if (password1 == "") {
            password1 = "admin";
        } else {
            Intent intent4 = getIntent();
            password1 = intent4.getStringExtra("oldpassw");
        }
        bpasschange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentoldpass = new Intent(MainActivity.this, PasswordChange.class);
                intentoldpass.putExtra("oldpassw", password1);
                Intent intentpasschan = new Intent(getApplicationContext(), PasswordChange.class);
                startActivity(intentpasschan);
            }
        });
        loginbutton.setOnClickListener(new View.OnClickListener() {
                                           @Override
                                           public void onClick(View view) {
                                               if (username.getText().toString().equals(username_string) &&
                                                       password.getText().toString().equals(password1)) {
                                                   Toast.makeText(getApplicationContext(),
                                                           "Redirecting...", Toast.LENGTH_SHORT).show();
                                                   Intent iop = new Intent(getApplicationContext(), ManagementPage.class);
                                                   startActivity(iop);

                                               } else {
                                                   Toast.makeText(getApplicationContext(),
                                                           "Wrong Entries", Toast.LENGTH_SHORT).show();
                                                   counter--;
                                                   if (counter == 0) {
                                                       loginbutton.setEnabled(false);
                                                   }

                                               }
                                           }
                                       }
        );

    }
}

密码更改.class

    package ahmedchtn.stockmanager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PasswordChange extends AppCompatActivity {
    Button bchange;
    EditText oldpassword;
    EditText newpassword;
    String oldpass;
    String newpass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_change);

        bchange = (Button) findViewById(R.id.bChange2);
        oldpassword = (EditText) findViewById(R.id.old_password);
        newpassword = (EditText) findViewById(R.id.new_password);
        bchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent ioldpass = getIntent();
                oldpass = ioldpass.getStringExtra("oldpassw");
                if (oldpass == (oldpassword.getText().toString())) {
                    newpass = newpassword.getText().toString();
                    Intent intentnew = new Intent(PasswordChange.this, MainActivity.class);
                    intentnew.putExtra("passnew", newpass);
                    startActivity(intentnew);
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
}

我认为问题出在意图上。提前感谢您的帮助!还有其他解决方案

日志猫

E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.NullPointerException
                      at ahmedchtn.stockmanager.MainActivity$2.onClick(MainActivity.java:46)
                      at android.view.View.performClick(View.java:4439)
                      at android.widget.Button.performClick(Button.java:139)
                      at android.view.View$PerformClick.run(View.java:18395)
                      at android.os.Handler.handleCallback(Handler.java:725)
                      at android.os.Handler.dispatchMessage(Handler.java:92)
                      at android.os.Looper.loop(Looper.java:176)
                      at android.app.ActivityThread.main(ActivityThread.java:5317)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:511)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                      at dalvik.system.NativeStart.main(Native Method)
Application terminated.

XML 代码activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ahmedchtn.stockmanager.MainActivity">

    <EditText
        android:id="@+id/identifer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:hint="@string/enter_your_id" />
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:hint="@string/enter_your_password"
        android:inputType="textPassword" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">
        <Button
            android:id="@+id/bLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/login" />
        <Button
            android:id="@+id/bCh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="changer passe" />
    </LinearLayout>

</LinearLayout>

activity_password_change.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_password_change"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="ahmedchtn.stockmanager.PasswordChange">
    <EditText
        android:id="@+id/old_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:hint="@string/enter_your_old_password" />
    <EditText
        android:id="@+id/new_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:hint="@string/enter_your_new_password" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:weightSum="1">
        <Button
            android:id="@+id/bChange2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:layout_weight="0.5"
            android:text="@string/confirm" />
        <Button
            android:id="@+id/main_page"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:layout_weight="0.5"
            android:text="@string/main_page" />

    </LinearLayout>
</LinearLayout>

清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ahmedchtn.stockmanager">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ManagementPage"
            android:parentActivityName=".MainActivity" />
        <activity android:name=".ProductsSearch" />
        <activity android:name=".ProductsCommand" />
        <activity
            android:name=".Editor"
            android:parentActivityName=".ProductsList" />
        <activity android:name=".ProductsList" />
        <provider
            android:name=".data.ProductProvider"
            android:authorities="ahmedchtn.stockmanager"
            android:exported="false" />
        <activity android:name=".PasswordChange"></activity>
    </application>
</manifest>

我已经更新了我的代码

从您到目前为止提供的信息(PasswordChange.class 第 28 行的 NPE)来看,似乎此行导致了问题:

 bchange = (Button) findViewById(R.id.bChange);
 bchange.setOnClickListener(new View.OnClickListener() {
 ...

您在两个布局 xml 文件中都有一个名为 bChange 的按钮 - 将其中一个更改为唯一的内容。我强烈建议所有组件都应通过应用程序唯一命名,因为这可以阻止活动意外引用错误资源的可能性。


例:

activity_password_change.xml

...
<Button
    android:id="@+id/bPasswordChange"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/activity_vertical_margin"
    android:layout_weight="0.5"
    android:text="@string/confirm" />
...

密码更改.java

...
bchange = (Button) findViewById(R.id.bPasswordChange);
...

对于MainActivity.java中出现的第二个问题:

Intent intentbool = getIntent();
bmain = intentbool.getBooleanExtra("bool", bmain);

但是,您的问题在于第 20 行:

Boolean bmain;

因为您声明的是 Boolean 对象而不是boolean基元(请注意大写字母 B),所以默认情况下会null它,因为您尚未初始化它。将其更改为原语:

boolean bmain;

确保在Manifest.xml文件中定义了 PasswordChange.class。

一个例子是<activity android:name=".PasswordChange"/>.这是应用程序在转到新活动时崩溃的最常见原因。

最新更新