我试图创建一个非常简单的登录页面,但是当我尝试启动应用程序时出现错误。 我根本没有得到它,因为我基本上就像我有另一个发射器一样。我已将意图过滤器放在这个新活动上。 任何帮助将不胜感激,因为我必须明天在我的考试中提出它。 这是代码。 关于插入的事情,现在只是,我稍后会改变它。
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
答案 0 :(得分:4)
loginButton.setOnClickListener(this);
您错过了findViewById
的{{1}}(对象初始化)
答案 1 :(得分:3)
您忘记初始化 loginButton 变量。
loginButton=(Button)findViewById(R.id.loginButton);
答案 2 :(得分:2)
你错过了
loginButton=(Button)findViewById(R.id.loginButton);
loginButton.setOnClickListener(this);