• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Dynamic Spinner with Database

LukeG224

Newbie
I am trying to have a layout with 4 spinners that dynamically change with each previous selection in a spinner.


Whenever I try and open my app, the app crashes and will never open. Here is a copy of my LogCat.
10-10 14:39:41.652: E/AndroidRuntime(1917): FATAL EXCEPTION: main
10-10 14:39:41.652: E/AndroidRuntime(1917): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.productguide/com.example.productguide.PowersportsEquivalent}: java.lang.NullPointerException
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.os.Looper.loop(Looper.java:137)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-10 14:39:41.652: E/AndroidRuntime(1917): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 14:39:41.652: E/AndroidRuntime(1917): at java.lang.reflect.Method.invoke(Method.java:525)
10-10 14:39:41.652: E/AndroidRuntime(1917): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-10 14:39:41.652: E/AndroidRuntime(1917): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 14:39:41.652: E/AndroidRuntime(1917): at dalvik.system.NativeStart.main(Native Method)
10-10 14:39:41.652: E/AndroidRuntime(1917): Caused by: java.lang.NullPointerException
10-10 14:39:41.652: E/AndroidRuntime(1917): at com.example.productguide.DataBaseHelper.getPowersportsType(DataBaseHelper.java:29)
10-10 14:39:41.652: E/AndroidRuntime(1917): at com.example.productguide.PowersportsEquivalent.onCreate(PowersportsEquivalent.java:27)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.app.Activity.performCreate(Activity.java:5133)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-10 14:39:41.652: E/AndroidRuntime(1917): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-10 14:39:41.652: E/AndroidRuntime(1917): ... 11 more​

I have no idea that what code I should post about the problem, if anyone has an idea, I will post the code accordingly.

-Thanks
 
It says you have a null pointer exception in your DataBaseHelper line 29.

10-10 14:39:41.652: E/AndroidRuntime(1917): Caused by: java.lang.NullPointerException
10-10 14:39:41.652: E/AndroidRuntime(1917): at com.example.productguide.DataBaseHelper.getPowersp ortsType(DataBaseHelper.java:29)
 
With this information:

It says you have a null pointer exception in your DataBaseHelper line 29.

10-10 14:39:41.652: E/AndroidRuntime(1917): Caused by: java.lang.NullPointerException
10-10 14:39:41.652: E/AndroidRuntime(1917): at com.example.productguide.DataBaseHelper.getPowersp ortsType(DataBaseHelper.java:29)

These are the lines of code where I get Null Pointer Exceptions

public static Cursor getPowersportsType(){

return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_TYPE}, null, null, POWERSPORTS_TYPE, null, null);
}

public static Cursor getPowersportsMake(){

return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_MAKE}, null, null, POWERSPORTS_MAKE, null, null);
}

public static Cursor getPowersportsYear(){

return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_YEARS}, null, null, POWERSPORTS_YEARS, null, null);
}

public static Cursor getPowersportsModel(){

return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_MODEL}, null, null, POWERSPORTS_MODEL, null, null);
}

I no longer have any crashing at the start up of my app but now I don't know how to fix my queries to populate my spinners.
 
Would need more code/info to determine if the queries aren't returning anything or if you aren't link them to the spinner properly.
 
DatabaseHelper.java

[HIGH]package com.example.productguide;

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

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/com.example.productguide/databases";
private static String DB_NAME = "powersports.db";
private final int DB_VERSION = 1;
private static SQLiteDatabase myDataBase;
private final Context myContext;
private static final String POWERSPORTS_TABLE = "powersports_data";
static final String POWERSPORTS_TYPE = "powersports_type";
static final String POWERSPORTS_MAKE = "powersports_make";
static final String POWERSPORTS_YEAR = "powersports_year";
static final String POWERSPORTS_MODEL = "powersports_model";

public DataBaseHelper(Context context){

super(context, DB_NAME, null, 1);
this.myContext = context;
}

public static Cursor getPowersportsType(){

return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_TYPE}, null, null, POWERSPORTS_TYPE, null, null);
}

public static Cursor getPowersportsMake(){

return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_MAKE}, null, null, POWERSPORTS_MAKE, null, null);
}

public static Cursor getPowersportsYear(){

return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_YEAR}, null, null, POWERSPORTS_YEAR, null, null);
}

public static Cursor getPowersportsModel(){

return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_MODEL}, null, null, POWERSPORTS_MODEL, null, null);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}

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

}[/HIGH]
 
As long as there is data in the table, those queries should return it. One way to verify that is to use "adb shell" and navigate to where the database is located. Then open the database using sqlite and execute those queries using SQL. Another would be to see what is in the cursor being returned. You should be able to set breakpoints and step through to find that data.

If you don't think there is an issue with the database or the cursor, it may be in how you're linking the cursor to the spinner and we'd need to see the code from that activity.
 
I used a tutorial for my already made database, maybe this is the problem. It doesn't seem to create my database in the database folder.
This was added to my DataBaseHelper beforehand and I didn't noticed that I posted the old code in my last post.


[HIGH]package com.example.productguide;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/com.example.productguide/databases/";
private static String DB_NAME = "powersports.db";
private final int DB_VERSION = 1;
private static SQLiteDatabase myDataBase;
private final Context myContext;
private static final String POWERSPORTS_TABLE = "powersports_data";
static final String POWERSPORTS_ID = "_id";
static final String POWERSPORTS_TYPE = "powersports_type";
static final String POWERSPORTS_MAKE = "powersports_make";
static final String POWERSPORTS_YEARS = "powersports_years";
static final String POWERSPORTS_MODEL = "powersports_model";

public DataBaseHelper(Context context){

super(context, DB_NAME, null, 1);
this.myContext = context;
}

//public static Cursor getPowersportsType(){
//
// return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_TYPE}, null, null, POWERSPORTS_TYPE, null, null);
//}

//public static Cursor getPowersportsMake(){
//
// return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_MAKE}, null, null, POWERSPORTS_MAKE, null, null);
//}

//public static Cursor getPowersportsYear(){
//
// return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_YEARS}, null, null, POWERSPORTS_YEARS, null, null);
//}

//public static Cursor getPowersportsModel(){
//
// return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_MODEL}, null, null, POWERSPORTS_MODEL, null, null);
//}



/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{

boolean dbExist = checkDataBase();

if(dbExist){
//do nothing - database already exist
}else{

//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();

try {

copyDataBase();

} catch (IOException e) {

throw new Error("Error copying database");

}
}

}

/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){

SQLiteDatabase checkDB = null;

try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}catch(SQLiteException e){

//database does't exist yet.

}

if(checkDB != null){

checkDB.close();

}

return checkDB != null ? true : false;
}

/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transferring bytestream.
* */
private void copyDataBase() throws IOException{

//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);

// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;

//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);

//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();

}

public void openDataBase() throws SQLException{

//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {

if(myDataBase != null)
myDataBase.close();

super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_POWERSPORTS_TABLE = "CREATE TABLE " + POWERSPORTS_TABLE + "("
+ POWERSPORTS_TYPE + " TEXT,"
+ POWERSPORTS_MAKE + " TEXT,"
+ POWERSPORTS_YEARS + " INT,"
+ POWERSPORTS_MODEL + "TEXT" + ")";
db.execSQL(CREATE_POWERSPORTS_TABLE);
}

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


// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.


}[/HIGH]
 
I apologize for the double post but my code now puts the already made database in the data/data/package/database directory. I needed to have code to initialize it in the onCreate() in my first activity. My new problem is now the code causes crashing.

Here is the NEW LogCat:

[HIGH]10-25 11:36:12.938: E/AndroidRuntime(1962): FATAL EXCEPTION: main
10-25 11:36:12.938: E/AndroidRuntime(1962): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.productguide/com.example.productguide.PowersportsEquivalent}: java.lang.NullPointerException
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.os.Looper.loop(Looper.java:137)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-25 11:36:12.938: E/AndroidRuntime(1962): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:36:12.938: E/AndroidRuntime(1962): at java.lang.reflect.Method.invoke(Method.java:525)
10-25 11:36:12.938: E/AndroidRuntime(1962): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-25 11:36:12.938: E/AndroidRuntime(1962): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-25 11:36:12.938: E/AndroidRuntime(1962): at dalvik.system.NativeStart.main(Native Method)
10-25 11:36:12.938: E/AndroidRuntime(1962): Caused by: java.lang.NullPointerException
10-25 11:36:12.938: E/AndroidRuntime(1962): at com.example.productguide.PowersportsEquivalent.onCreate(PowersportsEquivalent.java:53)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.app.Activity.performCreate(Activity.java:5133)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-25 11:36:12.938: E/AndroidRuntime(1962): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-25 11:36:12.938: E/AndroidRuntime(1962): ... 11 more[/HIGH]


Here is my other Activity:
[HIGH]package com.example.productguide;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.view.Menu;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

public class PowersportsEquivalent extends Activity {

DataBaseHelper myDbHelper;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_powersports_equivalent);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
// TODO Auto-generated catch block
throw new Error("Unable to create database");
}

try {

myDbHelper.openDataBase();

}catch(SQLException sqle){

throw sqle;

}

final Cursor vType;
final Cursor vMake;
final Cursor vYear;
final Cursor vModel;
final Spinner vTypeSpinner;
final Spinner vMakeSpinner;
final Spinner vYearSpinner;
final Spinner vModelSpinner;

//POWERSPORTS TYPE
vType = (Cursor) DataBaseHelper.getPowersportsType();
this.startManagingCursor(vType);

SimpleCursorAdapter scaType = SimpleCursorAdapter1(this, R.layout.activity_powersports_equivalent,vType,new String [] {DataBaseHelper.POWERSPORTS_TYPE},new int[]{R.id.textView01});
scaType.setDropDownViewResource(R.layout.activity_powersports_equivalent);
vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
vTypeSpinner.setAdapter(scaType);


//POWERSPORTS MAKE
vMake = (Cursor) DataBaseHelper.getPowersportsMake();
this.startManagingCursor(vMake);

SimpleCursorAdapter scaMake = SimpleCursorAdapter2(this, R.layout.activity_powersports_equivalent,vMake,new String [] {DataBaseHelper.POWERSPORTS_MAKE},new int[]{R.id.textView02});
scaMake.setDropDownViewResource(R.layout.activity_powersports_equivalent);
vMakeSpinner = (Spinner) findViewById(R.id.makeSpinner);
vMakeSpinner.setAdapter(scaMake);

//POWERSPORTS YEAR
vYear = (Cursor) DataBaseHelper.getPowersportsYear();
this.startManagingCursor(vYear);

SimpleCursorAdapter scaYear = SimpleCursorAdapter3(this, R.layout.activity_powersports_equivalent,vYear,new String [] {DataBaseHelper.POWERSPORTS_YEARS},new int[]{R.id.textView03});
scaYear.setDropDownViewResource(R.layout.activity_powersports_equivalent);
vYearSpinner = (Spinner) findViewById(R.id.yearSpinner);
vYearSpinner.setAdapter(scaYear);

//POWERSPORTS MODEL
vModel = (Cursor) DataBaseHelper.getPowersportsModel();
this.startManagingCursor(vModel);

SimpleCursorAdapter scaModel = SimpleCursorAdapter4(this, R.layout.activity_powersports_equivalent,vModel,new String [] {DataBaseHelper.POWERSPORTS_MODEL},new int[]{R.id.textView04});
scaModel.setDropDownViewResource(R.layout.activity_powersports_equivalent);
vModelSpinner = (Spinner) findViewById(R.id.modelSpinner);
vModelSpinner.setAdapter(scaModel);

}

private SimpleCursorAdapter SimpleCursorAdapter1(
PowersportsEquivalent powersportsEquivalent,
int activityPowersportsEquivalent, Cursor vType, String[] strings,
int[] is) {
// TODO Auto-generated method stub
return null;
}

private SimpleCursorAdapter SimpleCursorAdapter2(
PowersportsEquivalent powersportsEquivalent,
int activityPowersportsEquivalent, Cursor vMake, String[] strings,
int[] is) {
// TODO Auto-generated method stub
return null;
}

private SimpleCursorAdapter SimpleCursorAdapter3(
PowersportsEquivalent powersportsEquivalent,
int activityPowersportsEquivalent, Cursor vYears, String[] strings,
int[] is) {
// TODO Auto-generated method stub
return null;
}

private SimpleCursorAdapter SimpleCursorAdapter4(
PowersportsEquivalent powersportsEquivalent,
int activityPowersportsEquivalent, Cursor vModel, String[] strings,
int[] is) {
// TODO Auto-generated method stub
return null;
}

}[/HIGH]
 
The error is saying you have a null pointer exception at line 53 of your PowersportsEquivalent activity which has to do with the SimpleCursorAdapter.

I'm not sure what you're trying to do with the following code but it returns a null which is giving you the error:

Code:
[LIST=1]
[*]    private SimpleCursorAdapter SimpleCursorAdapter1(
[*]            PowersportsEquivalent powersportsEquivalent,
[*]            int activityPowersportsEquivalent, Cursor vType, String[] strings,
[*]            int[] is) {
[*]        // TODO Auto-generated method stub
[*]        return null;
[*]    }
[/LIST]
Instead of creating the adapter with this code:

Code:
[LIST=1]
[*]        SimpleCursorAdapter scaType = SimpleCursorAdapter1(this, R.layout.activity_powersports_equivalent,vType,new String [] {DataBaseHelper.POWERSPORTS_TYPE},new int[]{R.id.textView01});
[*]        scaType.setDropDownViewResource(R.layout.activity_powersports_equivalent);
[*]        vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
[*]        vTypeSpinner.setAdapter(scaType);
[/LIST]
Try this instead:

Code:
[LIST=1]
[*]        SimpleCursorAdapter scaType = new SimpleCursorAdapter(this, R.layout.activity_powersports_equivalent,vType,new String [] {DataBaseHelper.POWERSPORTS_TYPE},new int[]{R.id.textView01});
[*]        vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
[*]        vTypeSpinner.setAdapter(scaType);
[/LIST]
 
After replacing the code for my adapter with your provided code, I get a crash on start up of the app but for a different reason.

Here is the LogCat:
[HIGH]10-31 14:01:37.216: D/AndroidRuntime(1882): Shutting down VM
10-31 14:01:37.216: W/dalvikvm(1882): threadid=1: thread exiting with uncaught exception (group=0xb0f77648)
10-31 14:01:37.266: E/AndroidRuntime(1882): FATAL EXCEPTION: main
10-31 14:01:37.266: E/AndroidRuntime(1882): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.productguide/com.example.productguide.PowersportsEquivalent}: java.lang.IllegalArgumentException: column '_id' does not exist
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.os.Looper.loop(Looper.java:137)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-31 14:01:37.266: E/AndroidRuntime(1882): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 14:01:37.266: E/AndroidRuntime(1882): at java.lang.reflect.Method.invoke(Method.java:525)
10-31 14:01:37.266: E/AndroidRuntime(1882): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-31 14:01:37.266: E/AndroidRuntime(1882): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 14:01:37.266: E/AndroidRuntime(1882): at dalvik.system.NativeStart.main(Native Method)
10-31 14:01:37.266: E/AndroidRuntime(1882): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.widget.CursorAdapter.init(CursorAdapter.java:168)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.widget.CursorAdapter.<init>(CursorAdapter.java:116)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
10-31 14:01:37.266: E/AndroidRuntime(1882): at com.example.productguide.PowersportsEquivalent.onCreate(PowersportsEquivalent.java:53)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.app.Activity.performCreate(Activity.java:5133)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-31 14:01:37.266: E/AndroidRuntime(1882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-31 14:01:37.266: E/AndroidRuntime(1882): ... 11 more[/HIGH]

Is there something wrong with my database? I can clearly see a _id column within my database. Here is a screenshot of my columns:

 
I overlooked something when working with cursor adapters. Your query and the adapter needs to include the _id column.

You'll want to update each of your queries by adding the _id column like this:

public static Cursor getPowersportsType(){ return myDataBase.query(POWERSPORTS_TABLE, new String [] {POWERSPORTS_ID, POWERSPORTS_TYPE}, null, null, POWERSPORTS_TYPE, null, null); }

And update this line for the adapter:

SimpleCursorAdapter scaType = new SimpleCursorAdapter(this, R.layout.activity_powersports_equivalent,vType,new String [] {DataBaseHelper.POWERSPORTS_ID, DataBaseHelper.POWERSPORTS_TYPE},new int[]{R.id.textView01});
 
The app no longer crashes and I can see my data within the spinners.
Thank you.
Having the POWERSPORTS_ID within the SimpleCursorAdapters resulted in unwanted information,
and by removing them I get what I want. But another issue exists. In my spinners I have unwanted columns showing.

Here is a picture:



Also I would like it so that only the first spinner is populated at start-up and once it is selected
than the second spinner is populated depending on what was chosen first.
And that will repeat till the end.
How would I set up my selection Arguments? (I think that is what I have to use, right?)
 
I'm not sure I understand what's going wrong with the picture you posted. Would you mind posting the code of the PowersportsEquivalent activity with the changes you've made so far? It may also be an issue with the spinner layout xml file...

Regarding the population of the first spinner, I never found a way to have just the first spinner populate without the others loading as well. If you go that route, you will need to take the value from the first spinner and use that as an argument in the second query and so forth for each spinner.

I have an app that has vehicles estimated mpg which is somewhat similar to yours and when I first started developing it, I began like you did with spinners. But, I ended up having to go a different route because when the first spinner would initialize the other spinners would load as well. Making the user experience poor because the app would have to continually load spinners for each change.
 
Your code is actually what I based my app off of because you were doing
what I want to do. How did you end up doing your app in the end? I don't
mind if the spinners have to load with each change but if there is a better
way, I can try, and do it differently.

Here is the code:

[HIGH]package com.example.productguide;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.view.Menu;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

public class PowersportsEquivalent extends Activity {

DataBaseHelper myDbHelper;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_powersports_equivalent);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
// TODO Auto-generated catch block
throw new Error("Unable to create database");
}

try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;

}

final Cursor vType;
final Cursor vMake;
final Cursor vYear;
final Cursor vModel;
final Spinner vTypeSpinner;
final Spinner vMakeSpinner;
final Spinner vYearSpinner;
final Spinner vModelSpinner;

//POWERSPORTS TYPE
vType = (Cursor) DataBaseHelper.getPowersportsType();
this.startManagingCursor(vType);

SimpleCursorAdapter scaType = new SimpleCursorAdapter(this, R.layout.activity_powersports_equivalent,vType,new String [] {DataBaseHelper.POWERSPORTS_TYPE},new int[]{R.id.textView01});
vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
vTypeSpinner.setAdapter(scaType);

//POWERSPORTS MAKE
vMake = (Cursor) DataBaseHelper.getPowersportsMake();
this.startManagingCursor(vMake);

SimpleCursorAdapter scaMake = new SimpleCursorAdapter(this, R.layout.activity_powersports_equivalent,vMake,new String [] {DataBaseHelper.POWERSPORTS_MAKE},new int[]{R.id.textView02});
vMakeSpinner = (Spinner) findViewById(R.id.makeSpinner);
vMakeSpinner.setAdapter(scaMake);

//POWERSPORTS YEAR
vYear = (Cursor) DataBaseHelper.getPowersportsYear();
this.startManagingCursor(vYear);

SimpleCursorAdapter scaYear = new SimpleCursorAdapter(this, R.layout.activity_powersports_equivalent,vYear,new String [] {DataBaseHelper.POWERSPORTS_YEARS},new int[]{R.id.textView03});
vYearSpinner = (Spinner) findViewById(R.id.yearSpinner);
vYearSpinner.setAdapter(scaYear);

//POWERSPORTS MODEL
vModel = (Cursor) DataBaseHelper.getPowersportsModel();
this.startManagingCursor(vModel);

SimpleCursorAdapter scaModel = new SimpleCursorAdapter(this, R.layout.activity_powersports_equivalent,vModel,new String [] {DataBaseHelper.POWERSPORTS_MODEL},new int[]{R.id.textView04});
vModelSpinner = (Spinner) findViewById(R.id.modelSpinner);
vModelSpinner.setAdapter(scaModel);

}

private SimpleCursorAdapter SimpleCursorAdapter1(
PowersportsEquivalent powersportsEquivalent,
int activityPowersportsEquivalent, Cursor vType, String[] strings,
int[] is) {
// TODO Auto-generated method stub
return null;
}

private SimpleCursorAdapter SimpleCursorAdapter2(
PowersportsEquivalent powersportsEquivalent,
int activityPowersportsEquivalent, Cursor vMake, String[] strings,
int[] is) {
// TODO Auto-generated method stub
return null;
}

private SimpleCursorAdapter SimpleCursorAdapter3(
PowersportsEquivalent powersportsEquivalent,
int activityPowersportsEquivalent, Cursor vYears, String[] strings,
int[] is) {
// TODO Auto-generated method stub
return null;
}

private SimpleCursorAdapter SimpleCursorAdapter4(
PowersportsEquivalent powersportsEquivalent,
int activityPowersportsEquivalent, Cursor vModel, String[] strings,
int[] is) {
// TODO Auto-generated method stub
return null;
}

}[/HIGH]


Also, I really do appreciate the help. Thank you.
 
I ended up using separate ListViews for the user to make their selections. Take a look at: https://play.google.com/store/apps/details?id=com.estmpg.us

That app is old/ugly and certainly doesn't follow any of the current Android design guidelines but it works for me! It isn't popular but most of the apps I've made for my own use.

A lot of the database info that is easily searchable happens to be slightly out of date. I found some documentation on github about using a SQLiteCursorLoader and LoaderManager. That takes all the querying off of the UI thread and cleaned up some database errors that were occurring but I had to re-write a lot of code!
 
Each category should only have one field.
Type
all vehicle types
Make
all vehicle makes in a particular type from 1st spinner
Year(s)
all years for make, in particular type from 1st and 2nd spinner
Model
all models for particular year, in make, and type from 1st, 2nd and 3rd spinner.
 
I apologize for such a long delay. I am very busy with school and work.
Here is the xml layout:

[HIGH]<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PowersportsEquivalent" >

<TextView
android:id="@+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/vtype" />


<TextView
android:id="@+id/textView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView01"
android:layout_below="@+id/typeSpinner"
android:text="@string/vmake" />

<TextView
android:id="@+id/textView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView01"
android:layout_below="@+id/makeSpinner"
android:text="@string/vyear" />

<TextView
android:id="@+id/textView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView01"
android:layout_below="@+id/yearSpinner"
android:text="@string/vmodel" />

<Spinner
android:id="@+id/typeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView01"
android:layout_centerVertical="true"
android:layout_below="@+id/textView01" />

<Spinner
android:id="@+id/makeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView01"
android:layout_centerVertical="true"
android:layout_below="@+id/textView02" />

<Spinner
android:id="@+id/yearSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView01"
android:layout_centerVertical="true"
android:layout_below="@+id/textView03" />

<Spinner
android:id="@+id/modelSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView01"
android:layout_centerVertical="true"
android:layout_below="@+id/textView04" />

</RelativeLayout>[/HIGH]
 
I'm not seeing any problem with the xml either. If you're using Eclipse, you may try to do Project > Clean and then run the app again. I've had times where I changed the xml but it didn't work properly and doing the clean fixed it.
 
I cleaned the project and even made a new emulator to see if that was the problem with no luck. The layout is displayed just as it was in my previous post. Could there be something wrong with the way I am querying my databases? The queries are displaying the right item but it is also displaying the other columns in my database even though they shouldn't.

Actually I just noticed something. I assumed that since my app was opening correctly, I wasn't getting any errors. I actually get an error on the very first startup of the app on a new emulator.

Here is the LogCat:

[HIGH]11-07 15:25:04.463: E/SQLiteLog(2037): (14) cannot open file at line 30191 of [00bb9c9ce4]
11-07 15:25:04.463: E/SQLiteLog(2037): (14) os_unix.c:30191: (2) open(/data/data/com.example.lithiumbatteryguide/databases/powersports.db) -
11-07 15:25:04.483: E/SQLiteDatabase(2037): Failed to open database '/data/data/com.example.lithiumbatteryguide/databases/powersports.db'.
11-07 15:25:04.483: E/SQLiteDatabase(2037): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at com.example.lithiumbatteryguide.DataBaseHelper.checkDataBase(DataBaseHelper.java:95)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at com.example.lithiumbatteryguide.DataBaseHelper.createDataBase(DataBaseHelper.java:62)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at com.example.lithiumbatteryguide.PowersportsEquivalent.onCreate(PowersportsEquivalent.java:23)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.app.Activity.performCreate(Activity.java:5133)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.os.Looper.loop(Looper.java:137)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at java.lang.reflect.Method.invoke(Method.java:525)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 15:25:04.483: E/SQLiteDatabase(2037): at dalvik.system.NativeStart.main(Native Method)[/HIGH]

Even though the app opens with the right data from the database, the errors state that the database could not be opened. And also cannot open file at line 30191 of [00bb9c9ce4]
 
I see the issue for the layout. In your activity where you setup the SimpleCursorAdapters, you shouldn't use R.layout.activity_powersports_equivalent as the layout. It should be android.R.layout.simple_spinner_item. YOu want the data to go into the spinner and not the activity layout.

From that database error, it looks like your app is trying to open the database in /data/data/com.example.lithiumbatteryguide/databases/powersports.db

Which doesn't seem to match with what is in your DatabaseHelper. I'd set a breakpoint and step through to see where it got that address.
 
I actually have changed my app name (just for a test) to see if that would fix the problem and never mentioned it. BUT, I still get an error with the right pathway of /data/data/com.example.productguide/databases/powersports.db

I fixed the layout code for the spinners and they no longer have the extra columns that were not needed. But now no data is being shown in the spinners. No more errors with my database just no data is being shown in my spinners.

I will go over my database to see if I can find anything wrong.

As always, thanks for the help you have given.
 
After reviewing my database, it doesn't seem that anything is wrong with it. But I am still having a problem actually viewing my data within my spinners. Whats weird is that the app within the emulator shows the number of rows that I have. I have 5 choices for the first spinner and the first spinner in the emulator has 5 boxes when touched but they are all empty.

 
Back
Top Bottom