Android:无法启动活动异常

时间:2016-02-11 13:56:31

标签: android

我是Android编程新手。我正在尝试创建一个测验应用程序,但我经常遇到以下错误:

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.manasa.qfga / com.example.mns.qfga.Activity2}:java.lang.NullPointerException。

logcat显示为:

02-14 10:39:02.195 10021-10021/? D/dalvikvm: Late-enabling CheckJNI
02-14 10:39:02.355 10021-10021/? D/ActivityThread: handleBindApplication:com.example.manasa.qfga
02-14 10:39:02.355 10021-10021/? D/ActivityThread: setTargetHeapUtilization:0.75
02-14 10:39:02.355 10021-10021/? D/ActivityThread: setTargetHeapMinFree:2097152
02-14 10:39:02.385 10021-10021/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
02-14 10:39:02.385 10021-10021/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
02-14 10:39:02.385 10021-10021/? W/dalvikvm: VFY: unable to resolve interface method 17968: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
02-14 10:39:02.385 10021-10021/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
02-14 10:39:02.385 10021-10021/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
02-14 10:39:02.385 10021-10021/? W/dalvikvm: VFY: unable to resolve interface method 17972: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
02-14 10:39:02.385 10021-10021/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
02-14 10:39:02.395 10021-10021/? D/dalvikvm: GetMethodID: not returning static method Landroid/os/Process;.getTotalMemory ()J
02-14 10:39:02.395 10021-10021/? D/dalvikvm: GetMethodID: not returning static method Landroid/os/Process;.getFreeMemory ()J
02-14 10:39:02.455 10021-10021/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
02-14 10:39:02.455 10021-10021/? W/dalvikvm: VFY: unable to resolve virtual method 424: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-14 10:39:02.455 10021-10021/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-14 10:39:02.455 10021-10021/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
02-14 10:39:02.455 10021-10021/? W/dalvikvm: VFY: unable to resolve virtual method 446: Landroid/content/res/TypedArray;.getType (I)I
02-14 10:39:02.455 10021-10021/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-14 10:39:02.525 10021-10028/? D/dalvikvm: Debugger has detached; object registry had 1 entries
02-14 10:39:02.595 10021-10021/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
                                               OpenGL ES Shader Compiler Version: E031.24.02.07
                                               Build Date: 03/30/15 Mon
                                               Local Branch: 
                                               Remote Branch: 
                                               Local Patches: 
                                               Reconstruct Branch: 
02-14 10:39:02.815 10021-10021/? D/OpenGLRenderer: Enabling debug mode 0
02-14 10:39:03.105 10021-10021/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@4252b460 time:40547594
02-14 10:39:05.425 10021-10021/com.example.manasa.qfga I/Spinner: onItemSelected
02-14 10:39:05.425 10021-10021/com.example.manasa.qfga I/Timeline: Timeline: Activity_launch_request time:40549914
02-14 10:39:05.575 10021-10021/com.example.manasa.qfga D/ThemeValues: customized theme has contain this value: miui/color/action_bar_tab_text_color_normal_light/#b3ffffff
02-14 10:39:05.575 10021-10021/com.example.manasa.qfga D/ThemeValues: customized theme has contain this value: miui/color/action_mode_button_text_color_disable_light/#4dffffff
02-14 10:39:05.585 10021-10021/com.example.manasa.qfga D/ThemeValues: customized theme has contain this value: miui/color/action_mode_button_text_color_normal_light/#ffffffff
02-14 10:39:05.585 10021-10021/com.example.manasa.qfga D/ThemeValues: customized theme has contain this value: miui/color/action_mode_button_text_color_pressed_light/#ffffffff
02-14 10:39:05.585 10021-10021/com.example.manasa.qfga D/ThemeValues: customized theme has contain this value: miui/color/action_mode_button_text_color_selected_light/#ffffffff
02-14 10:39:05.585 10021-10021/com.example.manasa.qfga D/ThemeValues: customized theme has contain this value: miui/color/action_mode_title_text_color_light/#ffffffff
02-14 10:39:05.625 10021-10021/com.example.manasa.qfga D/testq: onCreate() of helper sql = CREATE TABLE questions (_id integer primary key autoincrement,question text,opt1 text,opt2 text,ans text)
02-14 10:39:07.365 10021-10021/com.example.manasa.qfga D/testq: getRandom5Qs() q = Question{question='Question94', opt1='opt1 94', opt2='opt2 94'}
02-14 10:39:07.375 10021-10021/com.example.manasa.qfga D/testq: getRandom5Qs() q = Question{question='Question15', opt1='opt1 15', opt2='opt2 15'}
02-14 10:39:07.375 10021-10021/com.example.manasa.qfga D/testq: getRandom5Qs() q = Question{question='Question42', opt1='opt1 42', opt2='opt2 42'}
02-14 10:39:07.375 10021-10021/com.example.manasa.qfga D/testq: getRandom5Qs() q = Question{question='Question2', opt1='opt1 2', opt2='opt2 2'}
02-14 10:39:07.375 10021-10021/com.example.manasa.qfga D/testq: getRandom5Qs() q = Question{question='Question24', opt1='opt1 24', opt2='opt2 24'}
02-14 10:39:07.375 10021-10021/com.example.manasa.qfga D/testq: onCreate() MA = [Question{question='Question94', opt1='opt1 94', opt2='opt2 94'}, Question{question='Question15', opt1='opt1 15', opt2='opt2 15'}, Question{question='Question42', opt1='opt1 42', opt2='opt2 42'}, Question{question='Question2', opt1='opt1 2', opt2='opt2 2'}, Question{question='Question24', opt1='opt1 24', opt2='opt2 24'}]
02-14 10:39:07.375 10021-10021/com.example.manasa.qfga D/AndroidRuntime: Shutting down VM
02-14 10:39:07.375 10021-10021/com.example.manasa.qfga W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4165bd58)
02-14 10:39:07.385 10021-10021/com.example.manasa.qfga E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.manasa.qfga, PID: 10021
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.manasa.qfga/com.example.manasa.qfga.Activity2}: java.lang.NullPointerException
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:141)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:136)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5113)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.NullPointerException
                                                                             at com.example.manasa.qfga.Activity2.onCreate(Activity2.java:44)
                                                                             at android.app.Activity.performCreate(Activity.java:5248)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:141) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:136) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5113) 
                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 
02-14 10:39:11.165 10021-10021/com.example.manasa.qfga I/Process: Sending signal. PID: 10021 SIG: 9

请帮我解决这个问题。

activity2.java

public class Activity2 extends AppCompatActivity {

    QuestionHelper helper;
    List<Question> questions;
    RadioGroup rdGp;
    RadioButton rd1,rd2;
    TextView tvQ;
    int pos =0;
    int result = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        tvQ = (TextView) findViewById(R.id.tvQ);
        rdGp = (RadioGroup) findViewById(R.id.rdGpOpts);
        rd1 = (RadioButton) findViewById(R.id.rd1);
        rd2 = (RadioButton) findViewById(R.id.rd2);

        helper = new QuestionHelper(this,QuestionHelper.DBNAME,null,QuestionHelper.DBVER);
        helper.addDummyQuestions();

        questions = helper.getRandom5Questions();

        Log.d("testq", "onCreate() MA = " + questions);

        Question firstQ = questions.get(pos);
        tvQ.setText(firstQ.getQuestion());
        rd1.setText(firstQ.getOpt1());
        rd2.setText(firstQ.getOpt2());
    }

    public void nextQ(View view) {
        Question q = questions.get(pos);

        if(rdGp.getCheckedRadioButtonId()==rd1.getId())
        {
            if(q.getAns().equals("opt1"))
                result++;
        }
        else
        {
            if(q.getAns().equals("opt2"))
                result++;
        }

        Toast.makeText(this, "num of correct ans = " + result, Toast.LENGTH_SHORT).show();

        pos++;

        Question nextQ = questions.get(pos);
        tvQ.setText(nextQ.getQuestion());
        rd1.setText(nextQ.getOpt1());
        rd2.setText(nextQ.getOpt2());
    }
} 

dbhelper

public class QuestionHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "questions";
    public static final int DBVER = 1;
    public static final String TBL_QUESTIONS = "questions";
    public static final String COL_ID = "_id";
    public static final String COL_Q = "question";
    public static final String COL_O1 = "opt1";
    public static final String COL_O2 = "opt2";
    public static final String COL_ANS = "ans";


    public QuestionHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE "+TBL_QUESTIONS+"("+COL_ID+" integer primary key autoincrement,"+COL_Q+" text,"+COL_O1+" text,"+COL_O2+" text,"+COL_ANS+" text)";
        Log.d("testq", "onCreate() of helper sql = " + sql);
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void addDummyQuestions()
    {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COL_Q,"Who is first PM of India");
        values.put(COL_O1,"Nehru");
        values.put(COL_O2,"Gandhiji");
        values.put(COL_ANS, "opt1");
        db.insert(TBL_QUESTIONS,null,values);
        values = new ContentValues();
        values.put(COL_Q,"Who is the best cricketer?");
        values.put(COL_O1,"Tendulkar");
        values.put(COL_O2,"ShaneWarne");
        values.put(COL_ANS, "opt1");
        db.insert(TBL_QUESTIONS,null,values);
        values = new ContentValues();
        values.put(COL_Q,"What has only been done by 12 men and no woman ever?");
        values.put(COL_O1,"Shoot ppl");
        values.put(COL_O2,"Men have landed 12 times on moon!");
        values.put(COL_ANS, "opt2");
        db.insert(TBL_QUESTIONS,null,values);


        for(int i = 0 ; i < 100 ; i++)
        {
            values = new ContentValues();
            values.put(COL_Q,"Question"+i);
            values.put(COL_O1,"opt1 "+i);
            values.put(COL_O2,"opt2 "+i);
            values.put(COL_ANS,"opt2");
            db.insert(TBL_QUESTIONS,null,values);
        }
    }

    public List<Question> getRandom5Questions()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        //db.query(TBL_QUESTIONS,null,null,null,null,null,null,)

        List<Question> list = new ArrayList<Question>();
        Cursor c = db.rawQuery("select * from " + TBL_QUESTIONS, null);
        int numRows = c.getCount();

        for(int i = 0 ; i < 5 ; i++)
        {
            int val = (int)(numRows * Math.random());
            c = db.rawQuery("select * from " + TBL_QUESTIONS + " limit 1 offset "+val, null);

            if (c.getCount() > 0) {
                c.moveToFirst();
                Question q = new Question();
                q.set_id(c.getInt(c.getColumnIndex(COL_ID)));
                q.setAns(c.getString(c.getColumnIndex(COL_ANS)));
                q.setOpt1(c.getString(c.getColumnIndex(COL_O1)));
                q.setOpt2(c.getString(c.getColumnIndex(COL_O2)));
                q.setQuestion(c.getString(c.getColumnIndex(COL_Q)));
                Log.d("testq", "getRandom5Qs() q = " + q);
                list.add(q);
            }
        }
        return list;
    }
}

1 个答案:

答案 0 :(得分:-1)

"CREATE TABLE "+TBL_QUESTIONS+"("+COL_ID+". It should be "CREATE TABLE "+TBL_QUESTIONS+" ("+COL_ID+"

中的表名后面缺少空格