Reputation: 2492
i'm trying to create a really simple login page, but I get an error when i try to launch the app. I don't get it at all, since that I've basically made it like I had the other launcher. And i have put the intent filter on this new activity. any help would be appreciated, since I have to Present it tomorrow at my exam. Here's the code. the thing about insert, is just for now, and I will change that later.
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','[email protected]');");
}
@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("");
}
}
here is the 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>
and here's the error message
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
Upvotes: 2
Views: 129
Reputation: 3339
You missed
loginButton=(Button)findViewById(R.id.loginButton);
loginButton.setOnClickListener(this);
Upvotes: 2
Reputation: 7449
You forgot to initialize loginButton variable.
loginButton=(Button)findViewById(R.id.loginButton);
Upvotes: 3
Reputation: 157487
loginButton.setOnClickListener(this);
you missed the findViewById
(object's initialization) for loginButton
Upvotes: 4