如何将内容转换为上下文

时间:2018-05-15 17:32:01

标签: android

logcat的

  

05-15 12:19:53.718 1622-1622 / com.ahmed.new_final E / AndroidRuntime:FATAL EXCEPTION:main                                                                      处理:com.ahmed.new_final,PID:1622                                                                      java.lang.ClassCastException:com.ahmed.new_final.activity.Teacher_add_updateActivity $ 1无法强制转换为android.content.Context                                                                          在com.ahmed.new_final.database.DBHeleper。(DBHeleper.java:31)                                                                          at com.ahmed.new_final.activity.Teacher_add_updateActivity $ 1.onClick(Teacher_add_updateActivity.java:46)                                                                          在android.view.View.performClick(View.java:4438)                                                                          在android.view.View $ PerformClick.run(View.java:18422)                                                                          在android.os.Handler.handleCallback(Handler.java:733)                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                          在android.os.Looper.loop(Looper.java:136)                                                                          在android.app.ActivityThread.main(ActivityThread.java:5001)                                                                          at java.lang.reflect.Method.invokeNative(Native Method)                                                                          在java.lang.reflect.Method.invoke(Method.java:515)                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)                                                                          在dalvik.system.NativeStart.main(本地方法)

DBhelper.java

public DBHeleper(View.OnClickListener context) {
    super((Context) context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

    sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_TEACHER + "( "
            + KEY_TEACHER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_TEACHER_NAME + " TEXT,"
            + KEY_TEACHER_POST + " TEXT,"
            + KEY_TEACHER_PHONE + " TEXT,"
            + KEY_TEACHER_EMAIL + " TEXT,"
            + KEY_TEACHER_OFFICE + " TEXT,"
            + KEY_TEACHER_OFFICE_HOURS + " TEXT)"
    );
}

Addteacher.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_teacher_add_update);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    edname = findViewById(R.id.edname);
    edpost = findViewById(R.id.edpost);
    edphone = findViewById(R.id.edphone);
    edemail = findViewById(R.id.edemail);
    edoffice= findViewById(R.id.edoffice);
    edofficehour= findViewById(R.id.edofficehour);


    FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = edname.getText().toString();
            String post = edpost.getText().toString();
            String phone = edphone.getText().toString();
            String email = edemail.getText().toString();
            String  office= edoffice.getText().toString();
            String officehour= edofficehour.getText().toString();

            Teacher teacher = new Teacher(name, post, phone, email, office, officehour);


            DBHeleper dbHeleper = new DBHeleper(this);

            long result= dbHeleper.AddTeacher(teacher);
            if(result != -1)
                Toast.makeText(Teacher_add_updateActivity.this, "Add", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(Teacher_add_updateActivity.this, "Faild", Toast.LENGTH_SHORT).show();
        }
    }); 

1 个答案:

答案 0 :(得分:1)

你不需要施放任何东西。 Context本身就是DBHeleper dbHeleper = new DBHeleper(Addteacher.this);

正确地说出这些陈述。

public DBHeleper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }

你的构造函数应该是

<script src="/socket.io/socket.io.js"></script>