2

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

4

2 回答 2

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]]

我会说你可能LinearLayout在你的layout文件中声明 a

<Linearlayout>

什么时候应该

<LinearLayout>

注意两个大写的“L”

并且您的终止标签可能是相同的方式。检查并修复它们,如果它们不正确。如果这不是问题,那么请发布您的activity_main.xml.

无关

在您onClick()中,您正在比较Views 但通常比较ids会更好,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;

通过这种方式,您可以比较ids,如果您愿意,它还允许您使用一个语句,恕我直言,如果您将此代码用于其他s switch,它看起来比一堆语句更干净。if/elseView

日志猫

在阅读您的 logcat 时查看此答案,这将极大地帮助您进行初步的自我调试,并让您更好地了解当您遇到困难时要发布的代码中最相关的部分是什么。

于 2013-10-28T02:24:34.593 回答
0

布局文件格式错误。

您有一个名为 a 的视图Linearlayout,而不是LinearLayoutLayout 的第 42 行

学习阅读 LogCat,因为它非常有助于准确地告诉您问题所在:

引起:android.view.InflateException:二进制 XML 文件第 42 行:膨胀类 Linearlayout 错误引起:java.lang.ClassNotFoundException:找不到类“android.view.Linearlayout”

编辑

编辑原始帖子后,您现在遇到另一个错误,与第一个完全无关:

引起:java.lang.NullPointerException MainActivity.onCreate(MainActivity.java:65)

根据您的 LogCat,这就是您现在的错误所在。如果它抛出错误,请尝试扫描您的 LogCat 以查找熟悉的名称和行号,正如我所说,它是一个非常好的调试工具,因为它可以准确地向您显示失败的地方。

于 2013-10-28T02:25:16.470 回答