mardi 28 juillet 2015

Android studio creating database

I am busy learning android programming. I have created a app where there is different buttons to click on and then a new activity is shown. The app worked 100% for clicking on the buttons. I now want to create a database. I followed a tutorial and done the same as in the tutorial to create the database. I now only want to create a database with one table with columns ID, Category, Heading and Info. When I run the app from android studio on my device connected to the PC I get the message when the app opens that the app stopped working.

My code for MainActivity.java

package app.mobiledevicesecurity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
DatabaseHelper myDb;
private static Button readbtn;
private static Button quizbtn;
private static Button scoresbtn;
private static Button settingsbtn;
private static Button helpbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDb = new DatabaseHelper(this);
     OnClickReadButtonListener();
     OnClickQuizButtonListener();
     OnClickScoresButtonListener();
     OnClickSettingsButtonListener();
     OnClickHelpButtonListener();
}

public void OnClickReadButtonListener() {
    readbtn = (Button) findViewById(R.id.readbutton);
    readbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Read_Category");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickQuizButtonListener() {
    quizbtn = (Button) findViewById(R.id.quizbutton);
    quizbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickScoresButtonListener() {
    scoresbtn = (Button) findViewById(R.id.scoresbutton);
    scoresbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Scores");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickSettingsButtonListener() {
    settingsbtn = (Button) findViewById(R.id.settingsbutton);
    settingsbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Settings");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickHelpButtonListener() {
    helpbtn = (Button) findViewById(R.id.helpbutton);
    helpbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Help");
                    startActivity(intent);
                }
            }
    );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Code for DatabaseHelper:

package app.mobiledevicesecurity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by Martin on 2015/07/28.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "mobilesec.db";
public static final String TABLE_NAME = "read_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "CATEGORY";
public static final String COL_3 = "HEADING";
public static final String COL_4 = "INFO";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table "+" (ID INTEGER PRIMARY KEY   AUTOINCREMENT,CATEGORY TEXT,HEADING TEXT,INFO TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}
}

Code for AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/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=".Read_Category"
        android:label="@string/title_activity_read__category" >
        <intent-filter>
            <action android:name="app.mobiledevicesecurity.Read_Category" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Quiz"
        android:label="@string/title_activity_quiz" >
        <intent-filter>
            <action android:name="app.mobiledevicesecurity.Quiz" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Scores"
        android:label="@string/title_activity_scores" >
        <intent-filter>
            <action android:name="app.mobiledevicesecurity.Scores" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Settings"
        android:label="@string/title_activity_settings" >
        <intent-filter>
            <action android:name="app.mobiledevicesecurity.Settings" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Help"
        android:label="@string/title_activity_help" >
        <intent-filter>
            <action android:name="app.mobiledevicesecurity.Help" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

 </manifest>

Any help would be appreciated

Aucun commentaire:

Enregistrer un commentaire