jeudi 4 février 2016

Error fetching resources from assests folder

I have recently started a new project, at the moment I am trying to create a SQLite database from within the main activity and read from a text file located in my assests folder. Here is my code:

package com.example.kingmarkmcc.universe;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

private Button simulator;
private Button helper;
private Button settings;
private Button milkyway;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    View.OnClickListener listener = new View.OnClickListener() {
        public void onClick(View v) {
            if (v == simulator) {
                Intent intent = new Intent(MainActivity.this, Simulator.class);
                startActivity(intent);
            } else if (v == helper) {
                Intent intent = new Intent(MainActivity.this, HelpOptions.class);
                startActivity(intent);
            } else if (v == settings) {
                Intent intent = new Intent(MainActivity.this, Settings.class);
                startActivity(intent);
            } else {
                Intent intent = new Intent(MainActivity.this, MilkyWay.class);
                startActivity(intent);
            }
        }
    };

    simulator = (Button) findViewById(R.id.buttonr1b1);
    simulator.setOnClickListener(listener);

    helper = (Button) findViewById(R.id.buttonr1b2);
    helper.setOnClickListener(listener);

    settings = (Button) findViewById(R.id.buttonr2b1);
    settings.setOnClickListener(listener);

    milkyway = (Button) findViewById(R.id.buttonr2b2);
    milkyway.setOnClickListener(listener);
}

@Override
protected void onStart()
{
    super.onStart();
    MainActivity myMain = new MainActivity();
    myMain.createDB();
}

@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);
}
private void createDB(){

    SQLiteDatabase db= null;
    final String dbName = "starDB";
    final String tableName = "starTable";
    String[] rowRead;
    String line;

    try {
        InputStream input;
        input = getAssets().open("refinedData.txt");
        InputStreamReader inputReader = new InputStreamReader(input);
        BufferedReader bufferReader = new BufferedReader(inputReader);
        //Retrieve or create DataBase
        db = this.openOrCreateDatabase(dbName, MODE_PRIVATE, null);
        //Creating table
        db.execSQL("CREATE TABLE IF NOT EXISTS "
                + tableName
                + " (id INTEGER, proper TEXT, dist INTEGER , absmag INTEGER , spect TEXT , ci INTEGER , x INTEGER , y INTEGER , z INTEGER , vx INTEGER , vy INTEGER , vz INTEGER);");
        while((line = bufferReader.readLine())!= null) {
            rowRead = line.split(",");
            // Put data into table
            db.execSQL("INSERT INTO "
                    + tableName
                    + " (id, proper, dist, absmag, spect, ci, x, y, z, vx, vy, vz)"
                    + " VALUES (" + rowRead[0] + "," + rowRead[1] + "," + rowRead[2] + "," + rowRead[3] + "," + rowRead[4] + "," + rowRead[5] + "," + rowRead[6] + "," + rowRead[7] + "," + rowRead[8] + "," + rowRead[9] + "," + rowRead[10] + "," + rowRead[11] + ");");
        }
        bufferReader.close();

        Cursor c = db.rawQuery("SELECT * FROM " + tableName, null);

        int Column1 = c.getColumnIndex("id");
        int Column2 = c.getColumnIndex("x");

        c.moveToFirst();

        int id = c.getInt(Column1);
        int x = c.getInt(Column2);

        System.out.println(id);
        System.out.println(x);

        Toast.makeText(MainActivity.this, "End", Toast.LENGTH_LONG).show();

    }
    catch(Exception e) {
        Log.e("Error", "Error", e);}

    finally {
        if (db != null)
            db.close();
    }
}

This is the error that I am receiving: (Had to place into pastebin link due to formatting errors, sorry) http://ift.tt/1oa56Kl

I have tried to correct this error but cannot seem to figure out the problem! Any help would be much appreciated

Aucun commentaire:

Enregistrer un commentaire