我正在尝试制作课程安排应用。但是,我遇到了一个问题,我不知道如何解决它。
我环顾四周,Google和StackOverflow尽可能找到了一些信息。但是,我没有得到它的工作。现在希望我可以获得有关代码的帮助。
我在Android开发方面相对缺乏经验。我的代码很难看。如果你能提高我的代码质量,我将非常感激你。
提前致谢。
MainActivity.java
package com.example.lee.myapplication;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Vector;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
private Vector<Course_lee> courseList;
private TextView tv_mainActivity;
@Bind(R.id.back)
Button back;
@Bind(R.id.week)
TextView week;
@Bind(R.id.set)
Button set;
@Bind(R.id.mon)
TextView mon;
@Bind(R.id.tue)
TextView tue;
@Bind(R.id.wed)
TextView wed;
@Bind(R.id.thur)
TextView thur;
@Bind(R.id.fri)
TextView fri;
@Bind(R.id.sta)
TextView sta;
@Bind(R.id.sun)
TextView sun;
@Bind(R.id.textView1_1)
TextView textView11;
@Bind(R.id.textView1_2)
TextView textView12;
@Bind(R.id.textView1_3)
TextView textView13;
@Bind(R.id.textView1_4)
TextView textView14;
@Bind(R.id.textView1_5)
TextView textView15;
@Bind(R.id.textView1_6)
TextView textView16;
@Bind(R.id.textView1_7)
TextView textView17;
@Bind(R.id.textView2_1)
TextView textView21;
@Bind(R.id.textView2_2)
TextView textView22;
@Bind(R.id.textView2_3)
TextView textView23;
@Bind(R.id.textView2_4)
TextView textView24;
@Bind(R.id.textView2_5)
TextView textView25;
@Bind(R.id.textView2_6)
TextView textView26;
@Bind(R.id.textView2_7)
TextView textView27;
@Bind(R.id.textView3_1)
TextView textView31;
@Bind(R.id.textView3_2)
TextView textView32;
@Bind(R.id.textView3_3)
TextView textView33;
@Bind(R.id.textView3_4)
TextView textView34;
@Bind(R.id.textView3_5)
TextView textView35;
@Bind(R.id.textView3_6)
TextView textView36;
@Bind(R.id.textView3_7)
TextView textView37;
@Bind(R.id.textView4_1)
TextView textView41;
@Bind(R.id.textView4_2)
TextView textView42;
@Bind(R.id.textView4_3)
TextView textView43;
@Bind(R.id.textView4_4)
TextView textView44;
@Bind(R.id.textView4_5)
TextView textView45;
@Bind(R.id.textView4_6)
TextView textView46;
@Bind(R.id.textView4_7)
TextView textView47;
@Bind(R.id.textView5_1)
TextView textView51;
@Bind(R.id.textView5_2)
TextView textView52;
@Bind(R.id.textView5_3)
TextView textView53;
@Bind(R.id.textView5_4)
TextView textView54;
@Bind(R.id.textView5_5)
TextView textView55;
@Bind(R.id.textView5_6)
TextView textView56;
@Bind(R.id.textView5_7)
TextView textView57;
@Bind(R.id.textView6_1)
TextView textView61;
@Bind(R.id.textView6_2)
TextView textView62;
@Bind(R.id.textView6_3)
TextView textView63;
@Bind(R.id.textView6_4)
TextView textView64;
@Bind(R.id.textView6_5)
TextView textView65;
@Bind(R.id.textView6_6)
TextView textView66;
@Bind(R.id.textView6_7)
TextView textView67;
@Bind(R.id.textView7_1)
TextView textView71;
@Bind(R.id.textView7_2)
TextView textView72;
@Bind(R.id.textView7_3)
TextView textView73;
@Bind(R.id.textView7_4)
TextView textView74;
@Bind(R.id.textView7_5)
TextView textView75;
@Bind(R.id.textView7_6)
TextView textView76;
@Bind(R.id.textView7_7)
TextView textView77;
@Bind(R.id.textView8_1)
TextView textView81;
@Bind(R.id.textView8_2)
TextView textView82;
@Bind(R.id.textView8_3)
TextView textView83;
@Bind(R.id.textView8_4)
TextView textView84;
@Bind(R.id.textView8_5)
TextView textView85;
@Bind(R.id.textView8_6)
TextView textView86;
@Bind(R.id.textView8_7)
TextView textView87;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lee);
ButterKnife.bind(this);
dbHelper = new MyDatabaseHelper(this, "CourseStore.db", null, 1);
SQLiteDatabase db =dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from course", null);
first_init(db,cursor);
init(cursor);
cursor.close();
}
private void first_init(SQLiteDatabase db,Cursor cursor) {
if (!cursor.moveToFirst()) {
Log.e("tag", String.valueOf(new Course_lee(R.id.textView1_1).getId()));
courseList.add(new Course_lee(R.id.textView1_1));
courseList.add(new Course_lee(R.id.textView1_2));
courseList.add(new Course_lee(R.id.textView1_3));
courseList.add(new Course_lee(R.id.textView1_4));
courseList.add(new Course_lee(R.id.textView1_5));
courseList.add(new Course_lee(R.id.textView1_6));
courseList.add(new Course_lee(R.id.textView1_7));
courseList.add(new Course_lee(R.id.textView2_1));
courseList.add(new Course_lee(R.id.textView2_2));
courseList.add(new Course_lee(R.id.textView2_3));
courseList.add(new Course_lee(R.id.textView2_4));
courseList.add(new Course_lee(R.id.textView2_5));
courseList.add(new Course_lee(R.id.textView2_6));
courseList.add(new Course_lee(R.id.textView2_7));
courseList.add(new Course_lee(R.id.textView3_1));
courseList.add(new Course_lee(R.id.textView3_2));
courseList.add(new Course_lee(R.id.textView3_3));
courseList.add(new Course_lee(R.id.textView3_4));
courseList.add(new Course_lee(R.id.textView3_5));
courseList.add(new Course_lee(R.id.textView3_6));
courseList.add(new Course_lee(R.id.textView3_7));
courseList.add(new Course_lee(R.id.textView4_1));
courseList.add(new Course_lee(R.id.textView4_2));
courseList.add(new Course_lee(R.id.textView4_3));
courseList.add(new Course_lee(R.id.textView4_4));
courseList.add(new Course_lee(R.id.textView4_5));
courseList.add(new Course_lee(R.id.textView4_6));
courseList.add(new Course_lee(R.id.textView4_7));
courseList.add(new Course_lee(R.id.textView5_1));
courseList.add(new Course_lee(R.id.textView5_2));
courseList.add(new Course_lee(R.id.textView5_3));
courseList.add(new Course_lee(R.id.textView5_4));
courseList.add(new Course_lee(R.id.textView5_5));
courseList.add(new Course_lee(R.id.textView5_6));
courseList.add(new Course_lee(R.id.textView5_7));
courseList.add(new Course_lee(R.id.textView6_1));
courseList.add(new Course_lee(R.id.textView6_2));
courseList.add(new Course_lee(R.id.textView6_3));
courseList.add(new Course_lee(R.id.textView6_4));
courseList.add(new Course_lee(R.id.textView6_5));
courseList.add(new Course_lee(R.id.textView6_6));
courseList.add(new Course_lee(R.id.textView6_7));
courseList.add(new Course_lee(R.id.textView7_1));
courseList.add(new Course_lee(R.id.textView7_2));
courseList.add(new Course_lee(R.id.textView7_3));
courseList.add(new Course_lee(R.id.textView7_4));
courseList.add(new Course_lee(R.id.textView7_5));
courseList.add(new Course_lee(R.id.textView7_6));
courseList.add(new Course_lee(R.id.textView7_7));
courseList.add(new Course_lee(R.id.textView8_1));
courseList.add(new Course_lee(R.id.textView8_2));
courseList.add(new Course_lee(R.id.textView8_3));
courseList.add(new Course_lee(R.id.textView8_4));
courseList.add(new Course_lee(R.id.textView8_5));
courseList.add(new Course_lee(R.id.textView8_6));
courseList.add(new Course_lee(R.id.textView8_7));
insertPrimaryTable(courseList,db);
}
}
private void insertPrimaryTable(Vector<Course_lee> courseList,SQLiteDatabase db) {
int id;
for (int i=0;i<courseList.size();i++){
id=courseList.elementAt(i).getId();
ContentValues values = new ContentValues();
values.put("id", id);
db.insert("course", null, values);
values.clear();
}
}
private void setWeek() {
}
private void init(Cursor cursor) {
//Cursor cursor = db.rawQuery("select * from course where day=? and time=?", new String[] {"1",""});
//Cursor cursor = db.rawQuery("select * from course",null);
if (cursor.moveToFirst()) {
do {
int id=cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String num = cursor.getString(cursor.getColumnIndex("num"));
String teacher = cursor.getString(cursor.getColumnIndex("teacher"));
int jieshu =cursor.getInt(cursor.getColumnIndex("jieshu"));
int weekBegin =cursor.getInt(cursor.getColumnIndex("weekBegin"));
int weekEnd =cursor.getInt(cursor.getColumnIndex("weekEnd"));
int flag=cursor.getInt(cursor.getColumnIndex("flag"));
String time=cursor.getString(cursor.getColumnIndex("time"));
int day = cursor.getInt(cursor.getColumnIndex("day"));
tv_mainActivity= (TextView) findViewById(id);
tv_mainActivity.setText(name);
if (!courseList.isEmpty()){
for (int i=0;i<courseList.size();i++){
if(courseList.elementAt(i).getId()==id){
courseList.elementAt(i).setName(name);
courseList.elementAt(i).setClassNum(num);
courseList.elementAt(i).setTeacher(teacher);
courseList.elementAt(i).setJieshu(jieshu);
courseList.elementAt(i).setWeekBegin(weekBegin);
courseList.elementAt(i).setWeekEnd(weekEnd);
courseList.elementAt(i).setFlag(flag);
courseList.elementAt(i).setTime(time);
courseList.elementAt(i).setDay(day);
break;
}
}
}
} while (cursor.moveToNext());
}
}
void highlight(){
}
void updateTable(String name,String num){
}
private void enterDetail(Course_lee course) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("course", course);
startActivity(intent);
}
@OnClick({R.id.back, R.id.set, R.id.textView1_1, R.id.textView1_2, R.id.textView1_3, R.id.textView1_4, R.id.textView1_5, R.id.textView1_6, R.id.textView1_7, R.id.textView2_1, R.id.textView2_2, R.id.textView2_3, R.id.textView2_4, R.id.textView2_5, R.id.textView2_6, R.id.textView2_7, R.id.textView3_1, R.id.textView3_2, R.id.textView3_3, R.id.textView3_4, R.id.textView3_5, R.id.textView3_6, R.id.textView3_7, R.id.textView4_1, R.id.textView4_2, R.id.textView4_3, R.id.textView4_4, R.id.textView4_5, R.id.textView4_6, R.id.textView4_7, R.id.textView5_1, R.id.textView5_2, R.id.textView5_3, R.id.textView5_4, R.id.textView5_5, R.id.textView5_6, R.id.textView5_7, R.id.textView6_1, R.id.textView6_2, R.id.textView6_3, R.id.textView6_4, R.id.textView6_5, R.id.textView6_6, R.id.textView6_7, R.id.textView7_1, R.id.textView7_2, R.id.textView7_3, R.id.textView7_4, R.id.textView7_5, R.id.textView7_6, R.id.textView7_7, R.id.textView8_1, R.id.textView8_2, R.id.textView8_3, R.id.textView8_4, R.id.textView8_5, R.id.textView8_6, R.id.textView8_7})
public void onClick(View view) {
Course_lee course;
switch (view.getId()) {
case R.id.back:
finish();
break;
case R.id.set:
break;
case R.id.textView1_1:
course=findCorrectCourse(R.id.textView1_1);
enterDetail(course);
break;
case R.id.textView1_2:
course=findCorrectCourse(R.id.textView1_2);
enterDetail(course);
break;
case R.id.textView1_3:
course=findCorrectCourse(R.id.textView1_3);
enterDetail(course);
break;
case R.id.textView1_4:
course=findCorrectCourse(R.id.textView1_4);
enterDetail(course);
break;
case R.id.textView1_5:
break;
case R.id.textView1_6:
break;
case R.id.textView1_7:
break;
case R.id.textView2_1:
break;
case R.id.textView2_2:
break;
case R.id.textView2_3:
break;
case R.id.textView2_4:
break;
case R.id.textView2_5:
break;
case R.id.textView2_6:
break;
case R.id.textView2_7:
break;
case R.id.textView3_1:
break;
case R.id.textView3_2:
break;
case R.id.textView3_3:
break;
case R.id.textView3_4:
break;
case R.id.textView3_5:
break;
case R.id.textView3_6:
break;
case R.id.textView3_7:
break;
case R.id.textView4_1:
break;
case R.id.textView4_2:
break;
case R.id.textView4_3:
break;
case R.id.textView4_4:
break;
case R.id.textView4_5:
break;
case R.id.textView4_6:
break;
case R.id.textView4_7:
break;
case R.id.textView5_1:
break;
case R.id.textView5_2:
break;
case R.id.textView5_3:
break;
case R.id.textView5_4:
break;
case R.id.textView5_5:
break;
case R.id.textView5_6:
break;
case R.id.textView5_7:
break;
case R.id.textView6_1:
break;
case R.id.textView6_2:
break;
case R.id.textView6_3:
break;
case R.id.textView6_4:
break;
case R.id.textView6_5:
break;
case R.id.textView6_6:
break;
case R.id.textView6_7:
break;
case R.id.textView7_1:
break;
case R.id.textView7_2:
break;
case R.id.textView7_3:
break;
case R.id.textView7_4:
break;
case R.id.textView7_5:
break;
case R.id.textView7_6:
break;
case R.id.textView7_7:
break;
case R.id.textView8_1:
break;
case R.id.textView8_2:
break;
case R.id.textView8_3:
break;
case R.id.textView8_4:
break;
case R.id.textView8_5:
break;
case R.id.textView8_6:
break;
case R.id.textView8_7:
break;
}
}
private Course_lee findCorrectCourse(int tv) {
Course_lee course;
for (int i=0;i<courseList.size();i++) {
if (courseList.elementAt(i).getId() == tv) {
course=courseList.elementAt(i);
return course;
}
}
return null;
}
}
Course_lee.java
package com.example.lee.myapplication;
import android.os.Parcel;
import android.os.Parcelable;
public class Course_lee implements Parcelable {
private int id;
private String name;
private String classNum;
private String teacher;
private int jieshu;
private int weekBegin;
private int weekEnd;
private int flag;
private String time;
private int day;
public Course_lee(int id, String name, String classNum, String teacher, int jieshu, int weekBegin, int weekEnd, int flag, String time, int day) {
this.id = id;
this.name = name;
this.classNum = classNum;
this.teacher = teacher;
this.jieshu = jieshu;
this.weekBegin = weekBegin;
this.weekEnd = weekEnd;
this.flag = flag;
this.time = time;
this.day = day;
}
public Course_lee(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public int getJieshu() {
return jieshu;
}
public void setJieshu(int jieshu) {
this.jieshu = jieshu;
}
public int getWeekBegin() {
return weekBegin;
}
public void setWeekBegin(int weekBegin) {
this.weekBegin = weekBegin;
}
public int getWeekEnd() {
return weekEnd;
}
public void setWeekEnd(int weekEnd) {
this.weekEnd = weekEnd;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.name);
dest.writeString(this.classNum);
dest.writeString(this.teacher);
dest.writeInt(this.jieshu);
dest.writeInt(this.weekBegin);
dest.writeInt(this.weekEnd);
dest.writeInt(this.flag);
dest.writeString(this.time);
dest.writeInt(this.day);
}
protected Course_lee(Parcel in) {
this.id = in.readInt();
this.name = in.readString();
this.classNum = in.readString();
this.teacher = in.readString();
this.jieshu = in.readInt();
this.weekBegin = in.readInt();
this.weekEnd = in.readInt();
this.flag = in.readInt();
this.time = in.readString();
this.day = in.readInt();
}
public static final Creator<Course_lee> CREATOR = new Creator<Course_lee>() {
@Override
public Course_lee createFromParcel(Parcel source) {
return new Course_lee(source);
}
@Override
public Course_lee[] newArray(int size) {
return new Course_lee[size];
}
};
}
MyDatabaseHelper.java
package com.example.lee.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_COURSE="create table course("
+"id integer primary key, "
+ "name text, "
+ "num text, "
+ "teacher text, "
+ "jieshu integer,"
+ "weekBegin integer,"
+ "weekEnd integer,"
+ "flag integer,"
+ "time text,"
+ "day integer)";
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_COURSE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
logcat的
03-25 11:36:34.647 10013-10013/com.example.lee.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lee.myapplication, PID: 10013
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lee.myapplication/com.example.lee.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.Vector.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.Vector.add(java.lang.Object)' on a null object reference
at com.example.lee.myapplication.MainActivity.first_init(MainActivity.java:181)
at com.example.lee.myapplication.MainActivity.onCreate(MainActivity.java:171)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
您没有初始化courselist
。在数据库交互之前将其添加到onCreate
private Vector<Course_lee> courseList = new Vector<Course_lee>();