(帮助)java.lang.runtimeexception无法启动活动组件信息

时间:2022-08-16 20:53:01

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

我是编程新手,如果这类问题已经被问过无数次了,我很抱歉。我对这些错误感到困惑,请帮助……这是我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

我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

最后这是我的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

我添加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

希望大家能帮助我解决我的app问题它告诉我错误在DBAdapter中java for code:“quiz.setJawaban_benar(cursor.getString(cursor.getColumnIndexOrThrow, COL_SOAL_JAWABAN_BENAR))”和MainActivity。Java代码:“mListQuiz = mDb.getAllSoal()”;main活动中的getAllSoal()方法取自DBAdapter.java

2 个解决方案

#1


3  

`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

注意到资本“L”年代

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.

你的终止标签可能也是一样的。如果错误,请检查并修复它们。如果这不是问题所在,那么请发布您的activity_main.xml。

Off-topic

八卦

In your onClick() you are comparing the Views but it is generally better to compare the ids of the View so it would look more like this

在onClick()中,您正在比较视图,但是通常比较视图的id会更好一些,这样看起来更像这样

 @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 ids 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 Views.

通过这种方式,您可以比较id,如果您想使用switch语句,IMHO看起来比if/else语句要干净,如果您将此代码用于其他视图的话。

Logcat

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.

在阅读logcat时查看这个答案,这将极大地帮助您进行初步的自我调试,并让您更好地理解当您陷入困境时,代码中最相关的部分是什么。

#2


0  

A layout file is malformed.

布局文件格式错误。

You have a view called a Linearlayout, instead of a LinearLayout on line #42 of your Layout

你有一个叫做线性布局的视图,而不是你布局的第42行中的线性布局

Learn to read LogCat, as it is very helpful at telling you exactly what the problem is:

学习阅读LogCat,因为它能帮助你准确地告诉你问题出在哪里:

Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class Linearlayout Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.Linearlayout"

引起的:android.view。反例:二进制XML文件行#42:错误膨胀类线性布局:java.lang。ClassNotFoundException:没有找到“android.view.Linearlayout”类

Edit

编辑

After editing your original post, you now have another error, completely unrelated to the first:

在编辑你的原始文章后,你现在有了另一个错误,与第一个完全无关:

Caused by: java.lang.NullPointerException MainActivity.onCreate(MainActivity.java:65)

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

According to your LogCat, this is where your error now lies. Please try to scan your LogCat for familiar names and line numbers if it throws errors, as I said, it is a really good tool for debugging, as it shows you exactly what failed where.

根据LogCat,这就是错误所在。如果LogCat出现错误,请尝试扫描您熟悉的名称和行号,正如我所说,它是一个非常好的调试工具,因为它向您显示了哪里出错了。

#1


3  

`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

注意到资本“L”年代

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.

你的终止标签可能也是一样的。如果错误,请检查并修复它们。如果这不是问题所在,那么请发布您的activity_main.xml。

Off-topic

八卦

In your onClick() you are comparing the Views but it is generally better to compare the ids of the View so it would look more like this

在onClick()中,您正在比较视图,但是通常比较视图的id会更好一些,这样看起来更像这样

 @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 ids 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 Views.

通过这种方式,您可以比较id,如果您想使用switch语句,IMHO看起来比if/else语句要干净,如果您将此代码用于其他视图的话。

Logcat

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.

在阅读logcat时查看这个答案,这将极大地帮助您进行初步的自我调试,并让您更好地理解当您陷入困境时,代码中最相关的部分是什么。

#2


0  

A layout file is malformed.

布局文件格式错误。

You have a view called a Linearlayout, instead of a LinearLayout on line #42 of your Layout

你有一个叫做线性布局的视图,而不是你布局的第42行中的线性布局

Learn to read LogCat, as it is very helpful at telling you exactly what the problem is:

学习阅读LogCat,因为它能帮助你准确地告诉你问题出在哪里:

Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class Linearlayout Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.Linearlayout"

引起的:android.view。反例:二进制XML文件行#42:错误膨胀类线性布局:java.lang。ClassNotFoundException:没有找到“android.view.Linearlayout”类

Edit

编辑

After editing your original post, you now have another error, completely unrelated to the first:

在编辑你的原始文章后,你现在有了另一个错误,与第一个完全无关:

Caused by: java.lang.NullPointerException MainActivity.onCreate(MainActivity.java:65)

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

According to your LogCat, this is where your error now lies. Please try to scan your LogCat for familiar names and line numbers if it throws errors, as I said, it is a really good tool for debugging, as it shows you exactly what failed where.

根据LogCat,这就是错误所在。如果LogCat出现错误,请尝试扫描您熟悉的名称和行号,正如我所说,它是一个非常好的调试工具,因为它向您显示了哪里出错了。