为什么我的添加按钮不起作用?

时间:2018-02-27 12:30:24

标签: android button

我正在编写一个简单的android程序,它接受主题的输入名称,然后在sqlite数据库中添加它们时打印它们。但我的添加按钮根本不起作用。帮我! 我跟随youtube上的newBoston课程,Bucky完成了工作!

我的主动文件

package com.akhilvaid21gmail.attendanceapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button=(Button) findViewById(R.id.moveOnButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToSecondActivity();

            }
        });

    }

private void goToSecondActivity()
{
    Intent intent=new Intent(this,Display.class);
    startActivity(intent);





}

我的main_activity_xml Xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.akhilvaid21gmail.attendanceapp.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="194dp"
        android:layout_height="225dp"
        android:text="Hello There!         Welcome to the Attendance App!"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.573"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.251" />

    <Button
        android:id="@+id/moveOnButton"
        android:layout_width="167dp"
        android:layout_height="76dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Check Attendance?"
        android:textColor="@color/colorAccent"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498" />

</android.support.constraint.ConstraintLayout>

我的Display.java文件

package com.akhilvaid21gmail.attendanceapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Display extends AppCompatActivity {



    EditText Input;
    TextView myText;
    myDBHandler dbHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);


        Input=(EditText)findViewById(R.id.inputText);

        myText= (TextView) findViewById(R.id.displayText);
        dbHandler= new myDBHandler(this,null,null,1);
        printDatabase();

    }
    //Print Database
    public void printDatabase()
    {
        String dbString=dbHandler.databaseToString();
        myText.setText(dbString);
        Input.setText("");

    }

    public void addButtonClicked(View view)
    {
        Attendance_Register attendance_register =new Attendance_Register(Input.getText().toString());
        dbHandler.addProduct(attendance_register);
    }
    //delete
    public void deleteButtonClicked(View view)
    {
        String InputText=Input.getText().toString();
        dbHandler.deleteSubject(InputText);

    }

}

我的Display.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.akhilvaid21gmail.attendanceapp.Display">

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="addButtonClicked"
        android:text="Add"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.153"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.314" />

    <Button
        android:id="@+id/deleteButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="deleteButtonClicked"
        android:text="Delete"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.753"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.317" />

    <EditText
        android:id="@+id/inputText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Enter your Subjects Name"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.017" />

    <TextView
        android:id="@+id/displayText"
        android:layout_width="174dp"
        android:layout_height="194dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.754" />
</android.support.constraint.ConstraintLayout>

我的myDBHandler文件

package com.akhilvaid21gmail.attendanceapp;

/**
 * Created by Akhil on 27-Feb-18.
 */

import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;


public class myDBHandler extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION=1;

    private static final String DATABASE_NAME="Attendance.db";
    public static final String TABLE_ATTENDANCE="Attendance";
    public static final String COLUMN_ID="_id";
    public static final String COLUMN_SUBJECTNAME="Subject Name";
    public myDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }



    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String query="CREATE TABLE "+   TABLE_ATTENDANCE+"("+
                COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT ,"+
               COLUMN_SUBJECTNAME+" TEXT "+
                ");";

            sqLiteDatabase.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTENDANCE);
        onCreate(sqLiteDatabase);

    }

    //Add new row to database
    public void addProduct( Attendance_Register attendance)
    {
        ContentValues values = new ContentValues();
        //,COLUMN_DAYS_PRESENT,attendance.getDays_present(),COLUMN_DAYS_ABSENT,attendance.getDays_absent()
        values.put(COLUMN_SUBJECTNAME,attendance.get_subjectName());
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        sqLiteDatabase.insert(TABLE_ATTENDANCE,null,values);
        sqLiteDatabase.close();


    }

    //Delete row from database
    public void deleteSubject(String subjectname)
    {
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        sqLiteDatabase.execSQL("DELETE FROM "+TABLE_ATTENDANCE+" WHERE "+ COLUMN_SUBJECTNAME+ "=\"" + subjectname+"\";");



    }


   // Print Data Base as a String
    public String databaseToString()
    {
        String dbString="";

        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        String query="SELECT * FROM "+TABLE_ATTENDANCE+" WHERE 1";
        //CURSOR POINT TO A LOCATION IN YOUR RESULTS
        Cursor c=sqLiteDatabase.rawQuery(query,null);
        //Move to first row
        c.moveToFirst();
            while(!c.isAfterLast())
            {
                if(c.getString(c.getColumnIndex("Subject Name "))!=null){
                    dbString+=c.getString((c.getColumnIndex("Subject Name")));
                    dbString+="\n";
                    c.moveToNext();
                }
            }

        sqLiteDatabase.close();
            return dbString;
    }




}

我的attendance_register文件

package com.akhilvaid21gmail.attendanceapp;

/**
 * Created by Akhil on 27-Feb-18.
 */

public class Attendance_Register {

    private int _id;

    private String _subjectName;

    public Attendance_Register(){

    }


    public Attendance_Register(String _subjectName) {
        this._subjectName = _subjectName;
    }

    public void set_id(int _id) {
        this._id = _id;
    }


    public void set_subjectName(String _subjectName) {
        this._subjectName = _subjectName;
    }


    public int get_id() {
        return _id;
    }


    public String get_subjectName() {
        return _subjectName;
    }

}

2 个答案:

答案 0 :(得分:1)

您没有引用在java文件(Display.java)中的xml中声明的Button

Button button1 = (Button) findViewById(R.id.addbutton);

答案 1 :(得分:0)

变化

EditText输入; 至 EditText inputText;

变量名称应以convention建议的小写字母开头。

要获得完整的结果,请详细分享,我没有让你出局。