我是Android的新手,我不知道为什么当我点击我的应用程序中的LOGIN按钮时,它显示应用程序已经停止。我已经多次检查了我的编码,但我仍然不知道问题在哪里,任何人都可以教我 ?我愿意学习:)(ps:另一个按钮,注册按钮效果很好)
这是main_activity.java
Button b1,b2;
EditText ed1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v){
b1=(Button)findViewById(R.id.blogin);
b2=(Button)findViewById(R.id.bsignup);
ed1=(EditText)findViewById(R.id.EVPassword);
if (v.getId() == R.id.blogin)
{
EditText a = (EditText)findViewById(R.id.ETmatrix);
String str = a.getText().toString();
ed1 = (EditText)findViewById(R.id.EVPassword);
String pass = ed1.getText().toString();
String password = helper.searchPass(str);
if(pass.equals(password))
{
Intent i = new Intent(MainActivity.this, HomePage.class);
i.putExtra("matrix", str);
startActivity(i);
}
else
{
Toast temp = Toast.makeText(MainActivity.this, "Matrix No. & Password does not match!!", Toast.LENGTH_SHORT);
temp.show();
}
}
if (v.getId() == R.id.bsignup )
{
Intent i = new Intent(MainActivity.this, SignUp.class);
startActivity(i);
}
}
}
main_activty.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/logo">
<TextView android:text="Login" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Planner"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:textSize="30dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/matrix"
android:hint="Matrix Number"
android:focusable="true"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
android:layout_below="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_alignRight="@+id/imageView"
android:layout_alignEnd="@+id/imageView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/password"
android:textColorHint="#ffff299f"
android:hint="Password"
android:layout_below="@+id/matrix"
android:layout_alignLeft="@+id/matrix"
android:layout_alignStart="@+id/matrix"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:text="login"
android:textAlignment="center"
android:id="@+id/blogin"
android:onClick="onButtonClick"
android:layout_alignTop="@+id/bsignup"
android:layout_alignLeft="@+id/password"
android:layout_alignStart="@+id/password" />
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Sign Up Here"
android:textAlignment="center"
android:id="@+id/bsignup"
android:onClick="onButtonClick"
android:layout_below="@+id/password"
android:layout_alignRight="@+id/password"
android:layout_alignEnd="@+id/password"
android:layout_marginTop="55dp" />
</RelativeLayout>
databasehelper.java
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "register.db";
private static final String TABLE_NAME = "register";
private static final String COLUMN_ID = "id";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PASS = "pass";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table register (id integer primary key not null ,"+
"uname text not null,email text not null,pass text not null)";
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private void OnCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
db.execSQL(query);
this.OnCreate(db);
}
public void insertRegister(Register r)
{
db = this.getWritableDatabase();
ContentValues values= new ContentValues();
String query = "select * from register";
Cursor cursor = db.rawQuery(query,null);
int count = cursor.getCount();
values.put(COLUMN_ID,count);
values.put(COLUMN_UNAME,r.getUname());
values.put(COLUMN_EMAIL,r.getEmail());
values.put(COLUMN_PASS, r.getPass());
db.insert(TABLE_NAME, null, values);
db.close();
}
public String searchPass(String uname)
{
db = this.getReadableDatabase();
String query = "select uname, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do{
a = cursor.getString(0);
if(a.equals(uname))
{
b = cursor.getString(1);
break;
}
}
while(cursor.moveToNext());
}
return b;
}
}
register.java
public class Register {
String uname, email, pass;
public void setUname(String uname) {this.uname = uname;}
public String getUname() {
return this.uname;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getPass() {
return this.pass;
}
}
logcat
12-05 23:29:02.604 7508-7508/? D/dalvikvm﹕ Late-enabling CheckJNI
12-05 23:29:02.628 7508-7514/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
12-05 23:29:02.664 7508-7508/? I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
12-05 23:29:02.664 7508-7508/? W/dalvikvm﹕ VFY: unable to resolve virtual method 238: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
12-05 23:29:02.664 7508-7508/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x00c8
12-05 23:29:02.668 7508-7508/? I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzj
12-05 23:29:02.668 7508-7508/? W/dalvikvm﹕ VFY: unable to resolve virtual method 533: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
12-05 23:29:02.668 7508-7508/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000b
12-05 23:29:02.760 7508-7525/? I/GMPM﹕ App measurement is starting up
12-05 23:29:02.784 7508-7525/? E/GMPM﹕ getGoogleAppId failed with status: 10
12-05 23:29:02.820 7508-7525/? E/GMPM﹕ Uploading is not possible. App measurement disabled
12-05 23:29:02.940 7508-7512/? D/dalvikvm﹕ GC_CONCURRENT freed 258K, 14% free 3652K/4224K, paused 6ms+1ms, total 129ms
12-05 23:29:02.964 7508-7508/? D/dalvikvm﹕ GC_FOR_ALLOC freed 57K, 14% free 3689K/4264K, paused 4ms, total 4ms
12-05 23:29:02.968 7508-7508/? I/dalvikvm-heap﹕ Grow heap (frag case) to 6.413MB for 2628012-byte allocation
12-05 23:29:02.976 7508-7517/? D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 9% free 6255K/6832K, paused 7ms, total 7ms
12-05 23:29:02.984 7508-7512/? D/dalvikvm﹕ GC_CONCURRENT freed <1K, 9% free 6255K/6832K, paused 3ms+0ms, total 8ms
12-05 23:29:03.184 7508-7508/? D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
12-05 23:29:03.184 7508-7508/? D/﹕ HostConnection::get() New Host Connection established 0xb8193208, tid 7508
12-05 23:29:03.196 7508-7508/com.example....... D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
12-05 23:29:03.196 7508-7508/com.example......... D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
12-05 23:29:03.344 7508-7508/com.example.........W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
12-05 23:29:03.344 7508-7508/com.example.........E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
12-05 23:29:03.348 7508-7508/com.example......... E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
12-05 23:29:03.400 7508-7508/com.example.........E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
12-05 23:29:03.404 7508-7508/com.example......... E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
12-05 23:29:03.404 7508-7508/com.example..........D/OpenGLRenderer﹕ Enabling debug mode 0
12-05 23:29:04.144 7508-7508/com.example.......... V/RenderScript﹕ 0xb81d1240 Launching thread(s), CPUs 4
12-05 23:29:08.044 7508-7508/com.example......... W/FileUtils﹕ Failed to chmod(/data/data/com.example........./databases/register.db): libcore.io.ErrnoException: chmod failed: EPERM (Operation not permitted)
12-05 23:29:08.048 7508-7508/com.example.........r E/SQLiteLog﹕ (1) no such column: uname
12-05 23:29:08.048 7508-7508/com.example.. D/AndroidRuntime﹕ Shut.......ting down VM
12-05 23:29:08.048 7508-7508/com.example......... W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4cb3b20)
12-05 23:29:08.048 7508-7508/com.example...... E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example......, PID: 7508
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
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: android.database.sqlite.SQLiteException: no such column: uname (code 1): , while compiling: select uname, pass from register
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at com.example........DatabaseHelper.searchPass(DatabaseHelper.java:69)
at com.example........MainActivity.onButtonClick(MainActivity.java:39)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
答案 0 :(得分:-1)
我知道你的问题。 定义这些:
Application#onCreate(...)
在onCreate()方法中。 结果将是:
b1=(Button)findViewById(R.id.blogin);
b2=(Button)findViewById(R.id.bsignup);
ed1=(EditText)findViewById(R.id.EVPassword);
如果这不起作用,请确保每个按钮/ editText的ID正确(Java区分大小写)并提供logcat(当应用程序崩溃时,请不要在设备上按“确定”。只需复制并在logcat中将所有文本粘贴为红色,然后按“确定”