I am new to programming and Im sorry if this type of question has been asked tons of times already. and i am so confused with those errors please help ... Here is My MainActivity.java
package id.romi.androidquiz;
import java.util.Collections;
import java.util.List;
import id.romi.androidquiz.entity.Quiz;
import id.romi.androidquiz.util.DBAdapter;
import id.romi.androidquiz.util.Utils;
import com.actionbarsherlock.app.SherlockActivity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends SherlockActivity implements OnClickListener
{
private static final String TAG = MainActivity.class.getName();
private TextView soal, user, txtScore, soalCounter, Timer;
private Button btnNext;
private RadioGroup rg_answer;
private RadioButton rb_A, rb_B, rb_C, rb_D;
private DBAdapter mDb;
private List<Quiz> mListQuiz;
private Quiz mQuiz;
private CountDownTimer mCountDownTimer;
private int mScore;
private int mTime = 0;
private int currentSoal = 0;
private static final int milisecond = 1000;
private static final int second = 90;
private static final int detik = second * milisecond;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//savedInstanceState instance dbAdapter
mDb = DBAdapter.getInstance(this);
// get data soal
mListQuiz = mDb.getAllSoal();
// acak list
Collections.shuffle(mListQuiz);
setupView();
// tampilkan input username
showInputUser();
}
private void mulaiQuiz()
{
setupSoal();
setupTimer();
}
private void showInputUser()
{
LayoutInflater mInflater = LayoutInflater.from(this);
View v = mInflater.inflate(R.layout.input_user, null);
final AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(v);
dialog.setTitle("Input Username");
final Button btnOk = (Button) v.findViewById(R.id.btnOk);
final EditText inputUser = (EditText) v.findViewById(R.id.inputUser);
btnOk.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
user.setText(inputUser.getText().toString());
mulaiQuiz();
dialog.dismiss();
}
});
dialog.show();
}
private void setupTimer()
{
mCountDownTimer = new CountDownTimer(detik, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
Timer.setText("time: " + millisUntilFinished / 1000 + "detik");
mTime = (int) (millisUntilFinished / 1000);
}
@Override
public void onFinish()
{
// TODO Auto-generated method stub
Timer.setText("time: 0 detik");
Toast.makeText(MainActivity.this, "Waktu Habis", Toast.LENGTH_SHORT).show();
}
};
mCountDownTimer.start();
}
private void setupSoal()
{
Utils.TRACE(TAG, "Soal ke - " + currentSoal);
Utils.TRACE(TAG, "Size - " + mListQuiz.size());
//clear checked radiobutton
rg_answer.clearCheck();
//get soal berdasar index
mQuiz = mListQuiz.get(currentSoal);
//set counter soal
soalCounter.setText("Soal ke -" + (currentSoal + 1));
//set soalnya
soal.setText(mQuiz.getSoal());
rb_A.setText("A. " + mQuiz.getJawaban_a());
rb_B.setText("B. " + mQuiz.getJawaban_b());
rb_C.setText("C. " + mQuiz.getJawaban_c());
rb_D.setText("D. " + mQuiz.getJawaban_d());
currentSoal++;
}
private void setupView()
{
soal = (TextView) findViewById(R.id.txtSoal);
soalCounter = (TextView) findViewById(R.id.txtSoalCount);
txtScore = (TextView) findViewById(R.id.txtScore);
user = (TextView) findViewById(R.id.txtUser);
Timer = (TextView) findViewById(R.id.timer);
txtScore.setText("Score : " + mScore);
rb_A = (RadioButton) findViewById(R.id.rb_A);
rb_B = (RadioButton) findViewById(R.id.rb_B);
rb_C = (RadioButton) findViewById(R.id.rb_C);
rb_D = (RadioButton) findViewById(R.id.rb_D);
rg_answer = (RadioGroup) findViewById(R.id.rgAnswer);
btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v == btnNext)
{
if(getAnswer().equals(mQuiz.getJawaban_benar().toUpperCase()))
{
mScore += 10;
txtScore.setText("Score" + mScore);
// setupSoal();
}
if(currentSoal < mListQuiz.size())
{
setupSoal();
}
else
{
mTime = second - mTime;
Bundle bundle = new Bundle();
bundle.putString("user", user.getText().toString());
bundle.putInt("score", mScore);
bundle.putInt("time", mTime);
Intent i = new Intent(MainActivity.this, ResultActivity.class);
i.putExtras(bundle);
startActivity(i);
finish();
}
Utils.TRACE(TAG, "Your score" + mScore);
}
}
private String getAnswer()
{
int id = rg_answer.getCheckedRadioButtonId();
if (id == R.id.rb_A )
{
return "A";
} else if (id == R.id.rb_B )
{
return "B";
} else if (id == R.id.rb_C )
{
return "C";
} else if (id == R.id.rb_D )
{
return "D";
}
return "";
}
}
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.romi.androidquiz"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >
<activity
android:name="id.romi.androidquiz.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="id.romi.androidquiz.ResultActivity"
android:label="@string/title_activity_result"
></activity>
</application>
</manifest>
And Finally this is my logcat
10-28 00:40:52.024: I/SQLiteAssetHelper(807): successfully opened database db_quiz
10-28 00:40:52.034: D/AndroidRuntime(807): Shutting down VM
10-28 00:40:52.034: W/dalvikvm(807): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-28 00:40:52.054: E/AndroidRuntime(807): FATAL EXCEPTION: main
10-28 00:40:52.054: E/AndroidRuntime(807): java.lang.RuntimeException: Unable to start activity ComponentInfo{id.romi.androidquiz/id.romi.androidquiz.MainActivity}: java.lang.NullPointerException
10-28 00:40:52.054: E/AndroidRuntime(807): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.os.Looper.loop(Looper.java:137)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-28 00:40:52.054: E/AndroidRuntime(807): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 00:40:52.054: E/AndroidRuntime(807): at java.lang.reflect.Method.invoke(Method.java:525)
10-28 00:40:52.054: E/AndroidRuntime(807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-28 00:40:52.054: E/AndroidRuntime(807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-28 00:40:52.054: E/AndroidRuntime(807): at dalvik.system.NativeStart.main(Native Method)
10-28 00:40:52.054: E/AndroidRuntime(807): Caused by: java.lang.NullPointerException
10-28 00:40:52.054: E/AndroidRuntime(807): at android.database.sqlite.SQLiteCursor.getColumnIndex(SQLiteCursor.java:178)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:301)
10-28 00:40:52.054: E/AndroidRuntime(807): at id.romi.androidquiz.util.DBAdapter.getAllSoal(DBAdapter.java:95)
10-28 00:40:52.054: E/AndroidRuntime(807): at id.romi.androidquiz.MainActivity.onCreate(MainActivity.java:65)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.app.Activity.performCreate(Activity.java:5133)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-28 00:40:52.054: E/AndroidRuntime(807): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-28 00:40:52.054: E/AndroidRuntime(807): ... 11 more
i add my DBAdapter.java
package id.romi.androidquiz.util;
import id.romi.androidquiz.entity.Quiz;
import java.util.ArrayList;
import java.util.List;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DBAdapter extends SQLiteAssetHelper {
private static final String DB_NAME ="db_quiz";
private static final int DB_VER = 1;
public static final String TABLE_SOAL ="tb_soal";
public static final String COL_SOAL_ID ="id";
public static final String COL_SOAL_SOAL ="soal";
public static final String COL_SOAL_JAWABAN_A ="jawaban_a";
public static final String COL_SOAL_JAWABAN_B ="soal";
public static final String COL_SOAL_JAWABAN_C ="soal";
public static final String COL_SOAL_JAWABAN_D ="soal";
private static DBAdapter dbInstance = null;
private static SQLiteDatabase db;
private String COL_SOAL_JAWABAN_BENAR;
private DBAdapter(Context context)
{
super(context, DB_NAME, null, DB_VER);
}
public static DBAdapter getInstance(Context context)
{
if(dbInstance == null)
{
dbInstance = new DBAdapter(context);
db = dbInstance.getWritableDatabase();
}
return dbInstance;
}
@Override
public synchronized void close()
{
super.close();
if (dbInstance != null)
{
dbInstance.close();
}
}
public List<Quiz> getAllSoal()
{
List<Quiz> listSoal = new ArrayList<Quiz>();
Cursor cursor = db.query(TABLE_SOAL, new String[]
{
COL_SOAL_ID,
COL_SOAL_SOAL,
COL_SOAL_JAWABAN_A,
COL_SOAL_JAWABAN_B,
COL_SOAL_JAWABAN_C,
COL_SOAL_JAWABAN_D,
COL_SOAL_JAWABAN_BENAR
}, null, null, null, null, null, null);
if (cursor.moveToFirst())
{
do
{
Quiz quiz = new Quiz();
quiz.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COL_SOAL_ID)));
quiz.setSoal(cursor.getString(cursor.getColumnIndexOrThrow(COL_SOAL_SOAL)));
quiz.setJawaban_a(cursor.getString(cursor.getColumnIndexOrThrow(COL_SOAL_JAWABAN_A)));
quiz.setJawaban_b(cursor.getString(cursor.getColumnIndexOrThrow(COL_SOAL_JAWABAN_B)));
quiz.setJawaban_c(cursor.getString(cursor.getColumnIndexOrThrow(COL_SOAL_JAWABAN_C)));
quiz.setJawaban_d(cursor.getString(cursor.getColumnIndexOrThrow(COL_SOAL_JAWABAN_D)));
quiz.setJawaban_benar(cursor.getString(cursor.getColumnIndexOrThrow(COL_SOAL_JAWABAN_BENAR)));
listSoal.add(quiz);
} while (cursor.moveToNext());
}
return listSoal;
}
}
Hope You all can Help me to solve my app problem
It tells that the errors are in DBAdapter.java for code : "quiz.setJawaban_benar(cursor.getString(cursor.getColumnIndexOrThrow(COL_SOAL_JAWABAN_BENAR)));"
and MainActivity.Java for code : "mListQuiz = mDb.getAllSoal();"
method getAllSoal() in MainActivity is taken from DBAdapter.java
`From this line in your logcat
10-27 05:30:40.517: E/AndroidRuntime(822): Caused by: java.lang.ClassNotFoundException:
Didn't find class "android.view.Linearlayout" on path:
DexPathList[[zip file "/data/app/id.romi.androidquiz-1.apk"],nativeLibraryDirectories=[/data/app-lib/id.romi.androidquiz-1, /system/lib]]
I would say that you probably declare a LinearLayout
in your layout
file as
<Linearlayout>
when it should be
<LinearLayout>
notice both capital "L"s
and your terminating tag is probably the same way. Check those and fix them if they are incorrect. If that isn't the problem then please post your activity_main.xml
.
Off-topic
In your onClick()
you are comparing the View
s but it is generally better to compare the id
s of the View
so it would look more like this
@Override
public void onClick(View v)
{
int id = v.getId(); // get the id of the View clicked then switch on it below
switch (id)
{
case R.id.btnNext:
//code if btnNext was clicked
break;
case R.id.idOfAnotherView:
// more code
break;
default:
//default code
break;
This way you are comparing the id
s and it also allows you to use a switch
statement if you want which, IMHO, looks cleaner than a bunch of if/else
statements if you will use this code for other View
s.
Logcat
Check out this answer on reading your logcat this will help tremendously in preliminary self-debugging and allow you to understand better what are the most relevant parts of your code to post when you are stuck.