我正在尝试制作一个测验应用。我从我观看过的YouTube视频中获取了代码。我完全复制了他所做的事情,当他运行它时,它可以正常工作,但在我的情况下,当我尝试按下一个问题时应用程序强制关闭...第一个问题出现,当我按下回车然后强行关闭。请帮助我,这是我的论文。
这里是logcat
java.lang.NullPointerException
at org.intercode.triviaquiz.Quiz$1.onClick(Quiz.java:50)
at android.view.View.performClick(View.java:4377)
at android.view.View$PerformClick.run(View.java:18031)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
代码
package org.intercode.triviaquiz;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class Quiz extends AppCompatActivity {
TextView tvquestion;
Button button;
RadioGroup rg;
RadioButton rb1, rb2, rb3;
String questions[]={"First Android Phone?", "Name of android version4.4?", "Android is which kind of software"};
String ans[]={"Motorola Droid","KitKat","Operating System"};
String opt[]={"HTC-G1", "HTC-One", "Motorola Droid", "JellyBean", "Froyo", "KitKat", "Operating System", "AntiVirus", "Application"};
int flag=0;
public static int marks, correct, wrong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tvquestion=(TextView)findViewById(R.id.tvquestion);
button=(Button)findViewById(R.id.button);
rg=(RadioGroup)findViewById(R.id.rg);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
rb3=(RadioButton)findViewById(R.id.rb3);
tvquestion.setText(questions[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton uans = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
if (ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
}
else
{
wrong++;
}
flag++;
if (flag < questions.length)
{
tvquestion.setText(questions[flag]);
rb1.setText(opt[flag * 3]);
rb2.setText(opt[flag * 3]);
rb3.setText(opt[flag * 3]);
}
else
{
if (Main.tbflag)
{
marks = correct - wrong;
} else
{
marks = correct;
}
Intent in = new Intent(getApplicationContext(), Results.class);
startActivity(in);
}
}
});
}
}
答案 0 :(得分:1)
LogCat之后
第一个问题是RadioGroup使用错误的资源ID进行初始化,并且为null,因此在调用rg.getCheckedRadioButtonId()
时会出现NullPointerException。
第二个问题是在启动活动时使用错误的Context
。
Intent in = new Intent(getApplicationContext(), Results.class);
参数getApplicationContext()
错误,意图应与Context
的{{1}}相关联,而不是Activity
。
将其替换为
Application