我得到了这个方法,它应该检查数据库中的给定数据,并用这些数据显示整行。
电影是表的名称,年份是列的名称。
我已经尝试了几个小时,我找不到理由,为什么会抛出这个:
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;
}
}
答案 0 :(得分:1)
正如MarsAtomic所指出的那样,你的db_ob并没有在整个代码中引用你的数据库,这就是导致空指针异常的原因。您似乎正在使用database_ob引用适配器类中的数据库。
也许您可以尝试在适配器类中创建一个方法来返回搜索功能的光标:
public Cursor querySearch(string sql, string[] selectionArgs)
{
Cursor c = database_ob.rawQuery(sql, selectionArgs);
return c;
}
并使用此
替换搜索功能中的第96行 Cursor cur = adapter_ob.querySearch("SELECT * FROM movies WHERE year LIKE ?", new String[] {searchx+"%"} );
我希望这有助于你的npe