gpt4 book ai didi

java - 启动器上出现空指针异常?安卓

转载 作者:太空狗 更新时间:2023-10-29 15:46:01 25 4
gpt4 key购买 nike

我正在尝试创建一个非常简单的登录页面,但是当我尝试启动该应用程序时出现错误。我根本不明白,因为我基本上已经做到了,就像我拥有另一个发射器一样。我已经将 Intent 过滤器放在这个新 Activity 上。任何帮助将不胜感激,因为我明天必须在考试中展示它。这是代码。关于插入的事情,只是暂时的,我稍后会改变它。

public class LoginActivity extends Activity implements OnClickListener {

EditText loginUsername,loginPassword;
Button loginButton;
SQLiteDatabase db;

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.login);
loginUsername=(EditText)findViewById(R.id.loginUsername);
loginPassword=(EditText)findViewById(R.id.loginPassword);
loginButton.setOnClickListener(this);
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS studentTest5(rollno VARCHAR,name VARCHAR,lastname VARCHAR,password VARCHAR,marks VARCHAR, phone VARCHAR, email VARCHAR);");
db.execSQL("INSERT INTO studentTest5 VALUES('1','Lars','Hansen','1234','7','60158096','habbahabba@hotmail.com');");
}


@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(view==loginButton)
{
if(loginUsername.getText().toString().trim().length()==0||
loginPassword.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
Cursor c=db.rawQuery("SELECT * FROM studentTest5 WHERE name='"+loginUsername.getText()+"'", null);
if(c.moveToFirst())
{
if(c.getString(3)==loginPassword.getText().toString())
{
showMessage("sucess", "user exists");
//loginPassword.setText(c.getString(3));
} else {
showMessage("Error", "incorrect password");
}


}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}

}

}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
loginUsername.setText("");
loginPassword.setText("");

}

}

这是xml

<TextView
android:id="@+id/loginWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:text="Welcome!"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/loginUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/loginWelcome"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Login"
android:inputType="textPersonName" />

<EditText
android:id="@+id/loginPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/loginUsername"
android:layout_alignRight="@+id/loginUsername"
android:layout_below="@+id/loginUsername"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/loginPassword"
android:layout_alignRight="@+id/loginPassword"
android:layout_below="@+id/loginPassword"
android:layout_marginTop="30dp"
android:text="Login" />

</RelativeLayout>

这是错误信息

   FATAL EXCEPTION: main
Process: com.technotalkative.viewstubdemo, PID: 10998
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.technotalkative.viewstubdemo/com.technotalkative.viewstubdemo.LoginActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.technotalkative.viewstubdemo.LoginActivity.onCreate(LoginActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
... 11 more

最佳答案

  loginButton.setOnClickListener(this);

您错过了 loginButtonfindViewById(对象的初始化)

关于java - 启动器上出现空指针异常?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994262/

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