该应用程序是一个简单的登录屏幕,它显示登录的学生出勤标记.....虽然它从未打开,只是说不幸这已停止,我可以找出原因!任何人都可以帮忙解决原因吗?
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.ac.qub.qubattend"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="uk.ac.qub.qubattend.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="uk.ac.qub.qubattend.StudentActivity"
android:label="@string/title_activity_student" >
</activity>
</application>
</manifest>
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="49dp"
android:layout_marginTop="69dp"
android:text="Student Number"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_centerVertical="true"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/textView2"
android:layout_marginTop="45dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText2"
android:layout_marginTop="42dp"
android:text="Login" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginTop="26dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
activity_student.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".StudentActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="72dp"
android:layout_marginTop="71dp"
android:text="Attendance"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/att"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="63dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
学生班
package uk.ac.qub.Student;
public class Student {
private String StudentId;
private String password;
private String week1;
private String week2;
private String week3;
private String week4;
private String week5;
private String week6;
private String week7;
private String week8;
private String week9;
private String week10;
private String week11;
private String week12;
public Student(){
}
public Student(String StudentId, String password, String week1,
String week2, String week3, String week4, String week5,
String week6, String week7, String week8, String week9,
String week10, String week11, String week12) {
this.StudentId = StudentId;
this.password = StudentId;
this.week1 = week1;
this.week1 = week2;
this.week1 = week3;
this.week1 = week4;
this.week1 = week5;
this.week1 = week6;
this.week1 = week7;
this.week1 = week8;
this.week1 = week9;
this.week1 = week10;
this.week1 = week11;
this.week1 = week12;
}
public String getStudentId() {
return StudentId;
}
public void setStudentId(String studentId) {
StudentId = studentId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getWeek1() {
return week1;
}
public void setWeek1(String week1) {
this.week1 = week1;
}
public String getWeek2() {
return week2;
}
public void setWeek2(String week2) {
this.week2 = week2;
}
public String getWeek3() {
return week3;
}
public void setWeek3(String week3) {
this.week3 = week3;
}
public String getWeek4() {
return week4;
}
public void setWeek4(String week4) {
this.week4 = week4;
}
public String getWeek5() {
return week5;
}
public void setWeek5(String week5) {
this.week5 = week5;
}
public String getWeek6() {
return week6;
}
public void setWeek6(String week6) {
this.week6 = week6;
}
public String getWeek7() {
return week7;
}
public void setWeek7(String week7) {
this.week7 = week7;
}
public String getWeek8() {
return week8;
}
public void setWeek8(String week8) {
this.week8 = week8;
}
public String getWeek9() {
return week9;
}
public void setWeek9(String week9) {
this.week9 = week9;
}
public String getWeek10() {
return week10;
}
public void setWeek10(String week10) {
this.week10 = week10;
}
public String getWeek11() {
return week11;
}
public void setWeek11(String week11) {
this.week11 = week11;
}
public String getWeek12() {
return week12;
}
public void setWeek12(String week12) {
this.week12 = week12;
}
}
主要活动
package uk.ac.qub.qubattend;
import java.util.ArrayList;
import uk.ac.qub.DBConnection.DBHelper;
import uk.ac.qub.Student.Student;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
EditText username;
EditText password;
TextView loginerror;
Button login;
ArrayList<Student> studentList;
static ArrayList<Student> currentStudent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBHelper db = new DBHelper(this);
studentList = (ArrayList<Student>)db.getAllStudents();
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
loginerror = (TextView)findViewById(R.id.textView3);
login = (Button)findViewById(R.id.button1);
login.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
String uname = username.getText().toString();
String pass = password.getText().toString();
switch (v.getId()) {
case R.id.button1:
for(Student s : studentList){
if(s.getStudentId().equals(uname) && s.getPassword().equals(pass)){
Intent i = new Intent(this, StudentActivity.class);
currentStudent.add(s);
startActivity(i);
} else {
loginerror.setText("Invalid Login Details");
username.setText("");
password.setText("");
}
break;}
default:
break;
}
}
}
StudentActivity类
package uk.ac.qub.qubattend;
import java.util.ArrayList;
import uk.ac.qub.Student.Student;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class StudentActivity extends Activity {
TextView attendance;
ArrayList<Student> cstudent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
attendance = (TextView)findViewById(R.id.att);
cstudent = (ArrayList<Student>) MainActivity.currentStudent;
for (Student s : cstudent) {
attendance.setText(s.getWeek1() + "\n" + s.getWeek2() + "\n"
+ s.getWeek3() + "\n" + s.getWeek4() + "\n" + s.getWeek5()
+ "\n" + s.getWeek6() + "\n" + s.getWeek7() + "\n"
+ s.getWeek8() + "\n" + s.getWeek9() + "\n" + s.getWeek10()
+ "\n" + s.getWeek11() + "\n" + s.getWeek12());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.student, menu);
return true;
}
}
DBHelper类
package uk.ac.qub.DBConnection;
import java.util.ArrayList;
import java.util.List;
import uk.ac.qub.Student.Student;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "QUBAttendanceDB";
// tasks table name
private static final String TABLE_StudentInfo = "studentinfo";
// Table Columns names
// id column
private static final String KEY_ID = "StudentId";
// question column
private static final String KEY_PASS = "question";
// answer column
private static final String KEY_WEEK1 = "Week1";
private static final String KEY_WEEK2 = "Week2";
private static final String KEY_WEEK3 = "Week3";
private static final String KEY_WEEK4 = "Week4";
private static final String KEY_WEEK5 = "Week5";
private static final String KEY_WEEK6 = "Week6";
private static final String KEY_WEEK7 = "Week7";
private static final String KEY_WEEK8 = "Week8";
private static final String KEY_WEEK9 = "Week9";
private static final String KEY_WEEK10 = "Week10";
private static final String KEY_WEEK11 = "Week11";
private static final String KEY_WEEK12 = "Week12";
// Database constant
private SQLiteDatabase dbase;
/**
* Method to check the db name and version
*
* @param context
*/
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase = db;
// Create Table method
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_StudentInfo + " ( "
+ KEY_ID + " TEXT" + KEY_PASS + " TEXT, " + KEY_WEEK1
+ "TEXT, " + KEY_WEEK2 + "TEXT, " + KEY_WEEK3 + "TEXT, "
+ KEY_WEEK4 + "TEXT, " + KEY_WEEK5 + "TEXT, " + KEY_WEEK6
+ "TEXT, " + KEY_WEEK7 + "TEXT, " + KEY_WEEK8 + "TEXT, "
+ KEY_WEEK9 + "TEXT, " + KEY_WEEK10 + "TEXT, " + KEY_WEEK11
+ "TEXT, " + KEY_WEEK12 + "TEXT, " + ")";
db.execSQL(sql);
// implementing add questions method
addAttendance();
}
/**
* Add Questions Method using Question Object with question, 4 options and
* correct answer
*/
private void addAttendance() {
Student s1 = new Student("40023798", "letmeinhere", "90", "80", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0");
this.addStudent(s1);
}
// Checking that existing database table called TABLE_QUEST is dropped
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_StudentInfo);
// Create tables again
onCreate(db);
}
/**
* method for adding a new question into the database
*
* @param quest
*/
public void addStudent(Student student) {
ContentValues values = new ContentValues();
values.put(KEY_ID, student.getStudentId());
values.put(KEY_PASS, student.getPassword());
values.put(KEY_WEEK1, student.getWeek1());
values.put(KEY_WEEK2, student.getWeek2());
values.put(KEY_WEEK3, student.getWeek3());
values.put(KEY_WEEK4, student.getWeek4());
values.put(KEY_WEEK5, student.getWeek5());
values.put(KEY_WEEK6, student.getWeek6());
values.put(KEY_WEEK7, student.getWeek7());
values.put(KEY_WEEK8, student.getWeek8());
values.put(KEY_WEEK9, student.getWeek9());
values.put(KEY_WEEK10, student.getWeek10());
values.put(KEY_WEEK11, student.getWeek11());
values.put(KEY_WEEK12, student.getWeek12());
// Inserting Row with question
dbase.insert(TABLE_StudentInfo, null, values);
}
/**
* Method to move questions in database to an ArrayList
*
* @return
*/
public List<Student> getAllStudents() {
List<Student> studentList = new ArrayList<Student>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_StudentInfo;
dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Student student = new Student();
student.setStudentId(cursor.getString(0));
student.setPassword(cursor.getString(1));
student.setWeek1(cursor.getString(2));
student.setWeek2(cursor.getString(3));
student.setWeek3(cursor.getString(4));
student.setWeek4(cursor.getString(5));
student.setWeek5(cursor.getString(6));
student.setWeek6(cursor.getString(7));
student.setWeek7(cursor.getString(8));
student.setWeek8(cursor.getString(9));
student.setWeek9(cursor.getString(10));
student.setWeek10(cursor.getString(11));
student.setWeek11(cursor.getString(12));
student.setWeek12(cursor.getString(13));
studentList.add(student);
} while (cursor.moveToNext());
}
// return question list
return studentList;
}
/**
* Method to Check Database number of rows
*
* @return
*/
public int rowcount() {
int row = 0;
// select all query
String selectQuery = "SELECT * FROM " + TABLE_StudentInfo;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row = cursor.getCount();
return row;
}
}
答案 0 :(得分:0)
您应该检查logcat
以查看异常。
我猜它在初始部分是一个简单的空指针错误。您可以从转储日志中跟踪它,在使用之前初始化该变量。