我是編程新手,我很抱歉,如果這種類型的問題已經問了很多次。 和我很困惑與這些錯誤,請幫助... 這裏是我的MainActivity.java(幫助)java.lang.runtimeexception無法啓動活動componentinfo
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 "";
}
}
我的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>
最後,這是我的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
我想添加我的DBAdapter.java
希望大家能幫助我解決我的應用程序的問題 它告訴錯誤是在DBAdapter.java代碼:"quiz.setJawaban_benar(cursor.getString(cursor.getColumnIndexOrThrow(COL_SOAL_JAWABAN_BENAR)));"
和MainActivity.Java代碼:"mListQuiz = mDb.getAllSoal();"
方法getAllSoal()在MainActivity從DBAdapter.java
你有文件'activity_main.xml'嗎?發佈..似乎有什麼東西在那裏失蹤.. –