I got this method which is supposed to check database for given data and display whole rows with this data.
movies is the name of the table, year is name of the column.
I've tried already for few hours and I can't find a reason, why it throws this:
11-24 19:45:47.269: E/AndroidRuntime(18564): FATAL EXCEPTION: main
11-24 19:45:47.269: E/AndroidRuntime(18564): java.lang.IllegalStateException: Could not execute method of the activity
11-24 19:45:47.269: E/AndroidRuntime(18564): at android.view.View$1.onClick(View.java:3633)
11-24 19:45:47.269: E/AndroidRuntime(18564): at android.view.View.performClick(View.java:4240)
11-24 19:45:47.269: E/AndroidRuntime(18564): at android.view.View$PerformClick.run(View.java:17721)
11-24 19:45:47.269: E/AndroidRuntime(18564): at android.os.Handler.handleCallback(Handler.java:730)
11-24 19:45:47.269: E/AndroidRuntime(18564): at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 19:45:47.269: E/AndroidRuntime(18564): at android.os.Looper.loop(Looper.java:137)
11-24 19:45:47.269: E/AndroidRuntime(18564): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-24 19:45:47.269: E/AndroidRuntime(18564): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 19:45:47.269: E/AndroidRuntime(18564): at java.lang.reflect.Method.invoke(Method.java:525)
11-24 19:45:47.269: E/AndroidRuntime(18564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-24 19:45:47.269: E/AndroidRuntime(18564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-24 19:45:47.269: E/AndroidRuntime(18564): at dalvik.system.NativeStart.main(Native Method)
11-24 19:45:47.269: E/AndroidRuntime(18564): Caused by: java.lang.reflect.InvocationTargetException
11-24 19:45:47.269: E/AndroidRuntime(18564): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 19:45:47.269: E/AndroidRuntime(18564): at java.lang.reflect.Method.invoke(Method.java:525)
11-24 19:45:47.269: E/AndroidRuntime(18564): at android.view.View$1.onClick(View.java:3628)
11-24 19:45:47.269: E/AndroidRuntime(18564): ... 11 more
11-24 19:45:47.269: E/AndroidRuntime(18564): Caused by: java.lang.NullPointerException
11-24 19:45:47.269: E/AndroidRuntime(18564): at com.example.imdbproject.MainActivity.search(MainActivity.java:96)
11-24 19:45:47.269: E/AndroidRuntime(18564): ... 14 more
...
package com.example.imdbproject;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MainActivity extends Activity {
ListView moviesList;
Button searchButton;
Button showAll;
Button addbtn;
Cursor cursor;
adapter adapter_ob;
MySQLiteHelper helper_ob;
SQLiteDatabase db_ob;
EditText searchET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moviesList = (ListView)findViewById(R.id.listView1);
searchButton =(Button)findViewById(R.id.searchButton);
addbtn = (Button)findViewById(R.id.addButton);
showAll = (Button)findViewById(R.id.showAllButton);
adapter_ob = new adapter(this);
searchET = (EditText)findViewById(R.id.searchEditText);
addbtn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent addsomemoviesIntent = new Intent(MainActivity.this, AddSomeMovies.class);
startActivity(addsomemoviesIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void show (View arg0)
{
String[] from = { helper_ob.KEY_TITLE, helper_ob.KEY_YEAR };
int[] to = { R.id.tv_title, R.id.tv_year };
cursor = adapter_ob.queryName();
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to, 1);
moviesList.setAdapter(cursorAdapter);
}
public void search (View arg0)
{
StringBuilder rtn = new StringBuilder();
String searchx = searchET.getText().toString();
adapter_ob.openToWrite();
Cursor cur = db_ob.rawQuery("SELECT * FROM movies WHERE year LIKE ?", new String[] {searchx+"%"} );
if (cur.moveToFirst()) {
do
{
if(cur.getString(1).equals(searchx))
rtn.append("'").append(cursor.getString(1)).append("' from ").append(cursor.getString(2));
} while (cur.moveToNext());
}
cursor.close();
adapter_ob.Close();
}
}
...
package com.example.imdbproject;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.database.Cursor;
public class adapter {
SQLiteDatabase database_ob;
MySQLiteHelper openHelper_ob;
Context context;
public adapter(Context c)
{
context = c;
}
public adapter openToRead()
{
openHelper_ob = new MySQLiteHelper(context,
MySQLiteHelper.DATABASE_NAME, null, MySQLiteHelper.DATABASE_VERSION);
database_ob = openHelper_ob.getReadableDatabase();
return this;
}
public adapter openToWrite()
{
openHelper_ob = new MySQLiteHelper(context,
MySQLiteHelper.DATABASE_NAME, null, MySQLiteHelper.DATABASE_VERSION);
database_ob = openHelper_ob.getWritableDatabase();
return this;
}
public void Close()
{
database_ob.close();
}
public long insertDetails(String title, String year)
{
ContentValues cv = new ContentValues();
cv.put(MySQLiteHelper.KEY_TITLE, title);
cv.put(MySQLiteHelper.KEY_YEAR, year);
openToWrite();
long val = database_ob.insert(MySQLiteHelper.TABLE_NAME, null, cv);
Close();
return val;
}
public Cursor queryName()
{
String[] cols = { MySQLiteHelper.KEY_ID, MySQLiteHelper.KEY_TITLE,
MySQLiteHelper.KEY_YEAR };
openToWrite();
Cursor c = database_ob.query(MySQLiteHelper.TABLE_NAME, cols, null, null, null, null, null);
return c;
}
}
package com.example.imdbproject;
import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "movie_data.db";
public static final int DATABASE_VERSION = 3;
public static final String TABLE_NAME = "movies";
public static final String KEY_ID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_YEAR = "year";
public static final String SCRIPT = "Create table " + TABLE_NAME + " ("
+ KEY_ID + " integer primary key autoincrement, " + KEY_TITLE
+ " text, " + KEY_YEAR + " text);";
public MySQLiteHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public MySQLiteHelper(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
}
@Override public void onCreate(SQLiteDatabase db)
{
db.execSQL(SCRIPT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE " + TABLE_NAME);
onCreate(db);
}
public long addMovie(String movietitle, String year){
ContentValues cv = new ContentValues();
cv.put(KEY_TITLE, movietitle);
cv.put(KEY_YEAR, year);
SQLiteDatabase sd = getWritableDatabase();
long result = sd.insert(TABLE_NAME, KEY_TITLE, cv);
return result;
}
}