我正在尝试运行此应用程序,并且每当我启动它时,它会发出错误(应用程序名称)已停止!
这是我的活动MainActivity.java的图片
package com.frns.salehnew;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
EditText id;
EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
id = (EditText)findViewById(R.id.tvId);
password = (EditText)findViewById(R.id.tvPassword);
bt.setOnClickListener(myhandler1);
}
View.OnClickListener myhandler1 = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Integer i = Integer.parseInt( id.getText().toString());
DBHelper mydb = new DBHelper(getApplicationContext());
Boolean check = mydb.CheckValidUser( i , password.getText().toString());
if( check )
{
Toast.makeText(getApplicationContext(), "welcome", Toast.LENGTH_LONG);
}
else
{
Toast.makeText(getApplicationContext(), "wrong id or password", Toast.LENGTH_LONG);
}
}
};
}
这是activity_main.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:gravity="top"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="158dp"
android:text="Submit" />
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="98dp"
android:text="TextView" />
<TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tvId"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="56dp"
android:text="TextView" />
</RelativeLayout>
这是我的活动DBhelper.java的图片
package com.frns.salehnew;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "tblusers";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_PASSWORD = "password";
public static final String CONTACTS_COLUMN_FNAME = "fname";
public static final String CONTACTS_COLUMN_lNAME = "lname";
public static final String CONTACTS_COLUMN_EMAIL = "email";
private HashMap hp;
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table tblusers " +
"(id integer primary key, password text,fname text,lname text, email text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS tblusers");
onCreate(db);
}
public boolean CheckValidUser (Integer id, String password)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor Res = db.rawQuery("select * from tblusers where id ="+id+"", null);
if( Res ==null)
{
return false;
}
else
{
Res.moveToFirst();
String p = Res.getString(Res.getColumnIndex(CONTACTS_COLUMN_PASSWORD));
if( p == password )
{
return true;
}
}
return false;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.frns.salehnew"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DBHelper"
android:label="@string/title_activity_dbhelper" >
</activity>
</application>
</manifest>
can anyone tell me why the "unfortunately application has stopped" error ?