我試圖通過方法傳遞一些與select *
查詢相關的值。我得到一個NullPointerException
。這是我的日誌:當我從列表中獲取值時出現NullPointerException錯誤
FATAL EXCEPTION: main
Process: dominio.android.forca, PID: 32241
java.lang.RuntimeException: Unable to start activity ComponentInfo{dominio.android.forca/dominio.android.forca.JogoAct}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List dominio.android.forca.data.AddressBookDatabaseHelper.Exemplo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List dominio.android.forca.data.AddressBookDatabaseHelper.Exemplo()' on a null object reference
at dominio.android.forca.data.Palavras.sorteio(Palavras.java:67)
at dominio.android.forca.JogoAct.iniciarJogo(JogoAct.java:195)
at dominio.android.forca.JogoAct.onCreate(JogoAct.java:39)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
這是我需要接受select *
數據的類:
public class Palavras extends ContentProvider {
private String[] palavras;
private List<String> lista_palavras;
public Palavras() {
}
// used to access the database
private AddressBookDatabaseHelper dbHelper;
@Override
public boolean onCreate() {
// create the AddressBookDatabaseHelper
dbHelper = new AddressBookDatabaseHelper(getContext());
return true; // ContentProvider successfully created
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
public String sorteio() {
//lista_palavras = dbHelper.Exemplo();
//String palavraSorteada = lista_palavras.get((int)(random()*lista_palavras.size()));
palavras = dbHelper.Exemplo2();
String palavraSorteada = palavras[(int)(random()*palavras.length)];
return palavraSorteada;
}
public static double random() {
Random r = new Random();
return r.nextDouble();
}
}
這是創建數據庫,並選擇一列功能:
class AddressBookDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Forca.db";
private static final int DATABASE_VERSION = 1;
// constructor
public AddressBookDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// creates the contacts table when the database is created
@Override
public void onCreate(SQLiteDatabase db) {
// SQL for creating the contacts table
final String CREATE_CONTACTS_TABLE =
"CREATE TABLE " + Contact.TABLE_NAME + "(" +
Contact._ID + " integer primary key, " +
Contact.COLUMN_WORD + " TEXT, " +
Contact.COLUMN_TIP + " TEXT);";
db.execSQL(CREATE_CONTACTS_TABLE); // create the contacts table
}
// normally defines how to upgrade the database when the schema changes
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// SELECT COLUMN_WORD through a list
public List<String> Exemplo(){
List<String> dados = new ArrayList();
SQLiteDatabase db = getReadableDatabase();
String selectQuery = "SELECT palavra FROM palavrasforca";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do{
String palavra = cursor.getString(cursor.getColumnIndex("palavra"));
dados.add(palavra);
}while (cursor.moveToNext());
}
return dados;
//aqui dados terá todos os valores do banco
}
}
這是我的AndroidManifest.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dominio.android.forca">
<application
android:allowBackup="true"
android:icon="@drawable/clown"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".PlayerAct">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".JogoAct" />
<activity android:name=".InserirPalavraAct" />
<activity android:name=".MainActivity" />
<provider
android:name=".data.AddressBookContentProvider"
android:authorities="dominio.android.forca.data"
android:enabled="true"
android:exported="false"></provider>
<activity android:name=".Autores"></activity>
</application>
</manifest>
這是我在JogoAct
類稱爲sorteio()
:
import dominio.android.forca.data.Palavras;
import dominio.android.forca.data.Replace;
public class JogoAct extends AppCompatActivity implements View.OnClickListener {
Palavras palavras = new Palavras();
private String palavraSecreta, tracos;
private int nTentativas;
private char letra;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jogo);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
txtDica = (TextView)findViewById(R.id.txtDica);
iniciarObjetos();
iniciarJogo();
}
public void iniciarJogo() {
palavraSecreta = palavras.sorteio(); // <<<<< HERE
nTentativas = 6;
tracos = "";
letra = ' ';
for (int x = 0; x < palavraSecreta.length(); x++) {
if (palavraSecreta.charAt(x) == '-') {
tracos += " - ";
}
else if(palavraSecreta.charAt(x) == ' '){
tracos += " ";
}
else {
tracos += " _ ";
}
}
habilitarBotoes();
tvPalavra.setText(tracos);
forca.setImageResource(R.drawable.forca_6);
}
爲什麼會出現這個錯誤?有什麼建議麼?
看起來像您直接調用內容提供者上的方法,而不是將其用作內容提供者 – njzk2
抱歉,但是,您是什麼意思? –
所以,這個錯誤發生在sorteio()中,但是如何調用呢? 另外,發佈您的AndroidManifest.xml文件。你忘了添加內容提供商嗎? – iheanyi