0
I have a problem that is seriously damaging the development of my application. It aims to draw a question (not implemented yet, I’m using a fixed value for the question id) through a query in Sqlite, and transmit to the Textviews. Here the code of the classes:
public class DBAdapter {
private SQLiteDatabase banco;
private DBHelper dbHelper;
public DBAdapter(Context context) {
dbHelper = new DBHelper(context);
}
private String[] allColumns = { DBHelper.ID, DBHelper.ALTA, DBHelper.ENUNC,
DBHelper.ALTB, DBHelper.ALTC, DBHelper.ALTD, DBHelper.ALTE, DBHelper.ALTOK};
public Cursor AcessarQuestao(int numquest, SQLiteDatabase bank){
Cursor cursorquestao;
bank = dbHelper.getWritableDatabase();
cursorquestao= bank.query(DBHelper.TABLE_NAME, allColumns, DBHelper.ID + " = " + numquest, null, null, null, null);
return cursorquestao;
}
}
public class QuestActivity extends AppCompatActivity {
Questao quest = new Questao();
private SQLiteDatabase bank;
TextView nuncp=(TextView)findViewById(R.id.nunciado);
TextView txt_altap=(TextView)findViewById(R.id.altea);
TextView txt_altbp=(TextView)findViewById(R.id.alteb);
TextView txt_altcp=(TextView)findViewById(R.id.altec);
TextView txt_altdp=(TextView)findViewById(R.id.alted);
TextView txt_altep=(TextView)findViewById(R.id.altee);
DBAdapter acesso = new DBAdapter(getBaseContext());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questoes);
TextView nunc=(TextView)findViewById(R.id.nunciado);
TextView txt_alta=(TextView)findViewById(R.id.altea);
TextView txt_altb=(TextView)findViewById(R.id.alteb);
TextView txt_altc=(TextView)findViewById(R.id.altec);
TextView txt_altd=(TextView)findViewById(R.id.alted);
TextView txt_alte=(TextView)findViewById(R.id.altee);
String password="txt";
Cursor obterquestao = acesso.AcessarQuestao(0, bank);
if( obterquestao != null && obterquestao.moveToFirst() ){
quest.setEnunc(obterquestao.getString(obterquestao.getColumnIndex(DBHelper.ENUNC)));
quest.setAlta(obterquestao.getString(obterquestao.getColumnIndex(DBHelper.ALTA)));
quest.setAltb(obterquestao.getString(obterquestao.getColumnIndex(DBHelper.ALTB)));
quest.setAltc(obterquestao.getString(obterquestao.getColumnIndex(DBHelper.ALTC)));
quest.setAltd(obterquestao.getString(obterquestao.getColumnIndex(DBHelper.ALTD)));
quest.setAlte(obterquestao.getString(obterquestao.getColumnIndex(DBHelper.ALTE)));
quest.setAltok(obterquestao.getString(obterquestao.getColumnIndex(DBHelper.ALTOK)));
}
nuncp.setText(quest.getEnunc());
txt_altap.setText(quest.getAlta());
txt_altbp.setText(quest.getAltb());
txt_altcp.setText(quest.getAltc());
txt_altdp.setText(quest.getAltd());
txt_altep.setText(quest.getAlte());
}/*
public void verificarResposta(View v) {
switch (v.getId()) {
case R.id.baltea:
quest.setResp(txt_altap.getText().toString());
if(quest.getResp().equals(quest.getAltok())){
Toast.makeText(getApplicationContext(), "Acertou!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.balteb:
quest.setResp(txt_altbp.getText().toString());
if(quest.getResp().equals(quest.getAltok())){
Toast.makeText(getApplicationContext(), "Acertou!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.baltec:
quest.setResp(txt_altcp.getText().toString());
if(quest.getResp().equals(quest.getAltok())){
Toast.makeText(getApplicationContext(), "Acertou!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.balted:
quest.setResp(txt_altdp.getText().toString());
if(quest.getResp().equals(quest.getAltok())){
Toast.makeText(getApplicationContext(), "Acertou!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.baltee:
quest.setResp(txt_altep.getText().toString());
if(quest.getResp().equals(quest.getAltok())){
Toast.makeText(getApplicationContext(), "Acertou!", Toast.LENGTH_SHORT).show();
}
break;
}
}*/
}
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "bancoquestoes.db";
public static final String TABLE_NAME = "quests";
private static final int DATABASE_VERSION = 1;
public static final String ID = "_id";
public static final String ENUNC = "enunc";
public static final String ALTA = "alta";
public static final String ALTB = "altb";
public static final String ALTC = "altc";
public static final String ALTD = "altd";
public static final String ALTE = "alte";
public static final String ALTOK = "altok";
private String[] allColumns = { DBHelper.ID, DBHelper.ALTA, DBHelper.ENUNC,
DBHelper.ALTB, DBHelper.ALTC, DBHelper.ALTD, DBHelper.ALTE, DBHelper.ALTOK};
private static final String CRIAR_TABELA="create table " + TABLE_NAME + "(" +
ID + " integer primary key autoincrement, " +
ENUNC + " text not null, " +
ALTA + " text not null, " +
ALTB + " text not null, " +
ALTC + " text not null, " +
ALTD + " text not null, " +
ALTE + " text not null, " +
ALTOK + " text not null);";
public DBHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CRIAR_TABELA);
ContentValues values = new ContentValues();
values.put(DBHelper.ENUNC, "Qual a resposta certa?");
values.put(DBHelper.ALTA, "Alternativa 1");
values.put(DBHelper.ALTB, "Alternativa 2");
values.put(DBHelper.ALTC, "Alternativa 3(ok)");
values.put(DBHelper.ALTD, "Alternativa 4");
values.put(DBHelper.ALTE, "Alternativa 5");
values.put(DBHelper.ALTOK, "Alternativa 3(ok)");
db.insert(TABLE_NAME, null, values );
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
However, in doing so, the following error occurs:
10-05 14:05:27.626 15291-15291/com.teacherfox.legendary.teacherfox E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.teacherfox.legendary.teacherfox/com.teacherfox.legendary.teacherfox.QuestActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2038) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139) at android.app.ActivityThread.access$700(ActivityThread.java:143) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4963) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:68) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:18) at com.teacherfox.legendary.teacherfox.QuestActivity.(QuestActivity.java:18) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1319) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139) at android.app.ActivityThread.access$700(ActivityThread.java:143) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4963) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) at dalvik.system.NativeStart.main(Native Method)
I’ve tried everything, but the mistake continues. Can anyone tell me what I’m doing wrong?
Edit: The Androidmanifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.teacherfox.legendary.teacherfox">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".QuestActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
You added the Activity
QuestActivity
in hisAndroidManifest.xml
? By mistake, probably this is the problem.– user28595
But it is not, when I comment the line of the method Accessquestao() the error does not happen. But I need to query the database daods.
– André Sousa
Add your
AndroidManifest.xml
editing the question, the problem originates by failing to declare Activity in this file and trying to invoke it in the application.– user28595
Added the manifest.
– André Sousa