简体   繁体   English

NPE导致Android应用程序崩溃

[英]Android application crashing due to NPE

I've made a question and answer application using Android Studio, however it keeps crashing. 我已经使用Android Studio创建了一个问答应用程序,但是它一直崩溃。 There are no errors shown on the side (it flags up green), but it crashes after the first time I try and submit an answer. 侧面没有显示任何错误(它标记为绿色),但是在我第一次尝试提交答案后便崩溃了。 I've found out that it's thrown a null pointer exception in the onClick method. 我发现它在onClick方法中引发了空指针异常。 I've had a look about and tried the solutions I could find but have been unable to fix this error so far. 我已经查看并尝试了我可以找到的解决方案,但到目前为止仍无法解决此错误。 Below is the code from the crashing page. 以下是崩溃页面中的代码。 Any help would be appreciated. 任何帮助,将不胜感激。

    package com.example.jeff.mygame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class QuestionActivity extends Activity {
    Intent intent;
    TextView tv;
    Button submitButton;
    RadioGroup radioGroup;
    RadioButton choice1, choice2, choice3, givenAnswer;
    String answersText;
    String questions[] = {"Which studio developed Elder Scrolls V: Skyrim?", "Who is the main character in the Mass Effect series?",
            "Which indie games company developed the Walking Dead series?"};
    String answers[] = {"Bethesda", "Commander Shepard", "TellTale Games"};
    String choices[] = {"Bioware", "Bethesda", "Ubisoft", "Commander Shepard", "Garrus Vakarian", "Illusive Man", "Team Meat", "Way Forward", "TellTale Games"};
    int flag = 0;
    public static int results, correct, incorrect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);

        tv = (TextView)findViewById(R.id.tvQ1);
        choice1 = (RadioButton)findViewById(R.id.rb1);
        choice2 = (RadioButton)findViewById(R.id.rb2);
        choice3 = (RadioButton)findViewById(R.id.rb3);
        submitButton = (Button)findViewById(R.id.submit1);

        tv.setText(questions[flag]);
        choice1.setText(choices[0]);
        choice2.setText(choices[1]);
        choice3.setText(choices[2]);
        submitButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                givenAnswer = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
                answersText = givenAnswer.getText().toString();

                if(answersText.equals(answers[flag])){
                    correct++;
                }else{
                    incorrect++;
                }flag++;
                if(flag < questions.length){
                    tv.setText(questions[flag]);
                    choice1.setText(choices[flag*3]);
                    choice2.setText(choices[flag*3]);
                    choice3.setText(choices[flag*3]);
                }else{
                    results = correct;
                    intent = new Intent(getApplicationContext(),FinishActivity.class);
                    startActivity(intent);
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_question, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Here's the logcat I forgot to add in at first. 这是我一开始忘记添加的logcat。

05-05 17:34:49.744      652-658/com.example.jeff.mygame I/dalvikvm﹕ threadid=3: reacting to signal 3
05-05 17:34:49.775      652-658/com.example.jeff.mygame I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
05-05 17:34:51.595      652-652/com.example.jeff.mygame I/Process﹕ Sending signal. PID: 652 SIG: 9
05-05 17:42:07.295      704-709/com.example.jeff.mygame I/dalvikvm﹕ threadid=3: reacting to signal 3
05-05 17:42:07.465      704-709/com.example.jeff.mygame I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
05-05 17:42:07.775      704-709/com.example.jeff.mygame I/dalvikvm﹕ threadid=3: reacting to signal 3
05-05 17:42:07.885      704-709/com.example.jeff.mygame I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
05-05 17:42:08.105      704-704/com.example.jeff.mygame D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
05-05 17:42:11.465      704-704/com.example.jeff.mygame D/AndroidRuntime﹕ Shutting down VM
05-05 17:42:11.465      704-704/com.example.jeff.mygame W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-05 17:42:11.475      704-704/com.example.jeff.mygame E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.jeff.mygame.QuestionActivity$1.onClick(QuestionActivity.java:48)
            at android.view.View.performClick(View.java:3511)
            at android.view.View$PerformClick.run(View.java:14105)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
05-05 17:42:12.015      704-709/com.example.jeff.mygame I/dalvikvm﹕ threadid=3: reacting to signal 3
05-05 17:42:12.025      704-709/com.example.jeff.mygame I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
05-05 17:42:15.525      704-704/com.example.jeff.mygame I/Process﹕ Sending signal. PID: 704 SIG: 9

You have not initialized the radioGroup . 您尚未初始化radioGroup You need to do as below 您需要执行以下操作

radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);  
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
                        // find which radio button is selected
                        if(checkedId == R.id.rb1) {
                        // do action
                       }
           }
});
button.setOnClickListener(new OnClickListener() { 
         @Override          
         public void onClick(View v) {
                        int selectedId = radioGroup.getCheckedRadioButtonId();
                        // find which radioButton is checked by id
                        if(selectedId == R.id.rb1 ) { 
                          // you have chosen so and so radio button
                        } 
                         // similarly check other buttons
         } 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM