gpt4 book ai didi

java - 游标/rawQuery 上的空指针异常

转载 作者:行者123 更新时间:2023-12-01 13:48:10 28 4
gpt4 key购买 nike

我得到了这个方法,它应该检查数据库中的给定数据并显示包含该数据的整行。

movies 是表的名称,year 是列的名称。

我已经尝试了几个小时,但找不到原因,为什么它会抛出此错误:

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;

}

}

最佳答案

正如 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 有帮助

关于java - 游标/rawQuery 上的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20183172/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com