2013-10-28 155 views
1

我是編程新手,我很抱歉,如果這種類型的問題已經問了很多次。 和我很困惑與這些錯誤,請幫助... 這裏是我的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

+0

你有文件'activity_main.xml'嗎?發佈..似乎有什麼東西在那裏失蹤.. –

回答

3

`從這個線在你的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]] 

我要說的是,你可能在你的layout文件中聲明一個LinearLayout作爲

<Linearlayout> 

當它應該是

<LinearLayout> 

通知資本 「L」 S

,你的終端標記可能是相同的方式。檢查並修復它們,如果它們不正確。如果這不是問題,那麼請發佈您的activity_main.xml

題外話

在你onClick()你比較View秒,但它通常是更好地比較id S中View,因此它看起來更像是這個

@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; 

這你比較id的方式,它也允許你使用switch聲明,如果你想要哪個,恕我直言,看起來比一堆if/else陳述更清潔,如果你是l使用此代碼用於其他View s。

logcat的

Check out this answer on reading your logcat這將極大地有助於初步的自我調試,使您更好地瞭解什麼,當你被卡住你的代碼的最相關部分發布。

+0

我已經變成了大寫,但仍然有logcat上的這些錯誤...現在它出現java.lang.nullpointerexception –

+0

然後,這是一個單獨的問題。有些東西是'空'的。看看我的關於如何讀取logcat的鏈接,它會告訴你哪一行有'null'。 – codeMagic

+0

檢查我的編輯帖子請高於 –

0

佈局文件格式不正確。

你有一個叫做Linearlayout上佈局的線#42視圖,而不是一個LinearLayout

學會閱讀logcat的,因爲它是非常有幫助的,告訴你到底是什麼問題:

造成的:android.view.InflateException:二進制XML文件行#42:拋出java.lang.ClassNotFoundException:錯誤充氣類的LinearLayout 所致找不到類 「android.view.Linearlayout」

編輯

編輯您的原職後,你現在有另一個錯誤,完全無關的第一:

產生的原因:顯示java.lang.NullPointerException MainActivity.onCreate(MainActivity.java: 65)

根據您的LogCat,這是您的錯誤現在的位置。如果它引發錯誤,請嘗試掃描您的LogCat以獲取熟悉的名稱和行號,正如我所說的,它是一個非常好的調試工具,因爲它顯示了哪裏失敗了。

+0

我已將其更改爲大寫但仍強制關閉 –

+0

新錯誤是什麼?你是否清理和重建項目? –

+0

我已經清除它...從logcat上的22個錯誤現在減少成爲11個更多的錯誤...它仍然是一樣的,應用程序總是強制關閉 –

相關問題