我在此应用中遇到问题,它向我显示“很遗憾,应用已停止”
MainActivity.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);
}
}
}
}
);
}
}
PasswordChange.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);
}
}
});
}
}
我认为问题在于意图.. 在此先感谢您的帮助 ! 还有其他解决方案
logcat
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>
我已更新我的代码
答案 0 :(得分:1)
根据您目前提供的信息(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" />
...
<强> PasswordChange.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;
答案 1 :(得分:0)
确保在Manifest.xml
文件中定义了PasswordChange.class。
一个例子是<activity android:name=".PasswordChange"/>
。这是应用程序在进行新活动时崩溃的最常见原因。