Java 无法启动活动?
在添加异步任务之前,我在代码中做了一些更改。我的应用程序工作正常,从远程服务器验证用户名和密码,但当登录成功消息消失时,无法启动其他活动。有人建议我现在添加异步任务,但当我输入正确的用户名和密码时,它就停止工作了。当我输入错误的用户名和密码时,它的工作状态会显示错误的用户名和密码消息。如果有人知道会发生什么错误,请帮助我 代码- 问题活性-Java 无法启动活动?,java,android,android-intent,login,android-asynctask,Java,Android,Android Intent,Login,Android Asynctask,在添加异步任务之前,我在代码中做了一些更改。我的应用程序工作正常,从远程服务器验证用户名和密码,但当登录成功消息消失时,无法启动其他活动。有人建议我现在添加异步任务,但当我输入正确的用户名和密码时,它就停止工作了。当我输入错误的用户名和密码时,它的工作状态会显示错误的用户名和密码消息。如果有人知道会发生什么错误,请帮助我 代码- 问题活性- public class QuestionActivity extends Activity implements OnClickListener{
public class QuestionActivity extends Activity implements OnClickListener{
private Question currentQ;
private GamePlay currentGame;
private CountDownTimer counterTimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
processScreen();
}
/**
* Configure current game and get question
*/
private void processScreen()
{
currentGame = ((CYKApplication)getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion();
Button nextBtn1 = (Button) findViewById(R.id.answer1);
nextBtn1.setOnClickListener(this);
Button nextBtn2 = (Button) findViewById(R.id.answer2);
nextBtn2.setOnClickListener(this);
Button nextBtn3 = (Button) findViewById(R.id.answer3);
nextBtn3.setOnClickListener(this);
Button nextBtn4 = (Button) findViewById(R.id.answer4);
nextBtn4.setOnClickListener(this);
Button nextBtn5 = (Button) findViewById(R.id.answer5);
nextBtn5.setOnClickListener(this);
/**
* Update the question and answer options..
*/
setQuestions();
}
/**
* Method to set the text for the question and answers from the current games
* current question
*/
private void setQuestions() {
//set the question text from current question
String question = Utility.capitalise(currentQ.getQuestion());
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
TextView option1 = (TextView) findViewById(R.id.answer1);
option1.setText(Utility.capitalise(answers.get(0)));
TextView option2 = (TextView) findViewById(R.id.answer2);
option2.setText(Utility.capitalise(answers.get(1)));
TextView option3 = (TextView) findViewById(R.id.answer3);
option3.setText(Utility.capitalise(answers.get(2)));
TextView option4 = (TextView) findViewById(R.id.answer4);
option4.setText(Utility.capitalise(answers.get(3)));
int score = currentGame.getScore();
String scr = String.valueOf(score);
TextView score1 = (TextView) findViewById(R.id.score);
score1.setText(scr);
counterTimer=new CountDownTimer(15000, 1000) {
public void onFinish() {
if(currentGame.getRound()==20)
System.exit(0);
currentGame.decrementScore();
processScreen();
}
public void onTick(long millisUntilFinished) {
TextView time = (TextView) findViewById(R.id.timers);
time.setText( ""+millisUntilFinished/1000);
}
};
counterTimer.start();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onClick(View arg0) {
//Log.d("Questions", "Moving to next question");
if(arg0.getId()==R.id.answer5)
{
new AlertDialog.Builder(this)
.setMessage("Are you sure?")
.setCancelable(true)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
}
}).setNegativeButton("No", null).show();
}
else
{
if(!checkAnswer(arg0)) return;
/**
* check if end of game
*/
if (currentGame.isGameOver()){
//Log.d("Questions", "End of game! lets add up the scores..");
//Log.d("Questions", "Questions Correct: " + currentGame.getRight());
//Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
Intent i = new Intent(this, EndgameActivity.class);
startActivity(i);
finish();
}
else
{
Intent i = new Intent(this, QuestionActivity.class);
finish();
startActivity(i);
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK :
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Check if a checkbox has been selected, and if it
* has then check if its correct and update gamescore
*/
private boolean checkAnswer(View v) {
final Button b = (Button) v;
String answer = b.getText().toString();
counterTimer.cancel();
b.setBackgroundResource(R.drawable.ans);
b.setEnabled(false);
//Log.d("Questions", "Valid Checkbox selection made - check if correct");
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
b.setBackgroundResource(R.drawable.ansgreen);
//Log.d("Questions", "Correct Answer!");
currentGame.incrementScore();
}
else{
b.setBackgroundResource(R.drawable.ansred);
//Log.d("Questions", "Incorrect Answer!");
currentGame.decrementScore1();
}
return true;
}
}
公共类QuestionActivity扩展活动实现OnClickListener{
私人问题;
私人游戏;
私人倒计时计数器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
processScreen();
}
/**
*配置当前游戏并获取问题
*/
私有void processScreen()
{
currentGame=((CykaApplication)getApplication()).getCurrentGame();
currentQ=currentGame.getNextQuestion();
按钮nextBtn1=(按钮)findViewById(R.id.answer1);
nextBtn1.setOnClickListener(这个);
按钮nextBtn2=(按钮)findViewById(R.id.answer2);
nextBtn2.setOnClickListener(此);
按钮nextBtn3=(按钮)findViewById(R.id.answer3);
nextBtn3.setOnClickListener(这个);
按钮nextBtn4=(按钮)findViewById(R.id.answer4);
nextBtn4.setOnClickListener(这个);
按钮nextBtn5=(按钮)findViewById(R.id.answer5);
nextBtn5.setOnClickListener(此);
/**
*更新问题和答案选项。。
*/
设置问题();
}
/**
*方法为当前游戏中的问题和答案设置文本
*当前问题
*/
私人问题{
//从当前问题设置问题文本
字符串question=Utility.capitalize(currentQ.getQuestion());
TextView qText=(TextView)findViewById(R.id.question);
qText.setText(问题);
//设置可用选项
列表答案=currentQ.getQuestionOptions();
TextView选项1=(TextView)findViewById(R.id.answer1);
选项1.setText(Utility.capitalize(answers.get(0));
TextView选项2=(TextView)findViewById(R.id.answer2);
选项2.setText(Utility.capitalize(answers.get(1));
TextView选项3=(TextView)findViewById(R.id.answer3);
选项3.setText(实用程序.大写(答案.获取(2));
TextView选项4=(TextView)findViewById(R.id.answer4);
选项4.setText(实用程序.capitalize(answers.get(3));
int score=currentGame.getScore();
String scr=String.valueOf(分数);
TextView得分1=(TextView)findViewById(R.id.score);
得分1.setText(scr);
计数器计时器=新的倒计时(15000,1000){
public void onFinish(){
如果(currentGame.getRound()==20)
系统出口(0);
currentGame.decrementScore();
processScreen();
}
公共void onTick(长毫秒未完成){
TextView时间=(TextView)findViewById(R.id.timers);
time.setText(“+millisuntillfinished/1000);
}
};
counterTimer.start();
}
@凌驾
恢复时公开作废(){
super.onResume();
}
@凌驾
公共void onClick(视图arg0){
//日志d(“问题”,“转到下一个问题”);
if(arg0.getId()==R.id.answer5)
{
新建AlertDialog.Builder(此)
.setMessage(“您确定吗?”)
.setCancelable(真)
.setPositiveButton(“是”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int id){
完成();
}
}).setNegativeButton(“否”,null).show();
}
其他的
{
如果(!checkAnswer(arg0))返回;
/**
*检查游戏是否结束
*/
if(currentGame.isGameOver()){
//Log.d(“问题”,“游戏结束!让我们把分数加起来…”);
//Log.d(“问题”,“问题正确:”+currentGame.getRight());
//Log.d(“问题”,“问题错误:+currentGame.getError());
意向i=新意向(此,EndgameActivity.class);
星触觉(i);
完成();
}
其他的
{
意图i=新意图(此,QuestionActivity.class);
完成();
星触觉(i);
}
}
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件)
{
开关(钥匙代码)
{
case KeyEvent.KEYCODE\u返回:
返回true;
}
返回super.onKeyDown(keyCode,event);
}
/**
*检查是否选中了复选框,以及
*然后检查其是否正确并更新游戏分数
*/
私有布尔校验应答(视图v){
最终按钮b=(按钮)v;
字符串应答=b.getText().toString();
计数器。取消();
b、 挫折资源(R.drawable.ans);
b、 setEnabled(假);
//日志d(“问题”,“选中有效复选框-检查是否正确”);
if(currentQ.getAnswer().equalsIgnoreCase(answer))
{
b、 挫折资源(R.drawable.ansgreen);
//Log.d(“问题”,“正确答案!”);
currentGame.incrementScore();
}
否则{
b、 挫折资源(R.drawable.ansred);
//Log.d(“
09-24 07:59:32.818: E/AndroidRuntime(17602): FATAL EXCEPTION: main
09-24 07:59:32.818: E/AndroidRuntime(17602): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.cyk/com.abc.cyk.QuestionActivity}: java.lang.NullPointerException
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.os.Looper.loop(Looper.java:137)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-24 07:59:32.818: E/AndroidRuntime(17602): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 07:59:32.818: E/AndroidRuntime(17602): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 07:59:32.818: E/AndroidRuntime(17602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-24 07:59:32.818: E/AndroidRuntime(17602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-24 07:59:32.818: E/AndroidRuntime(17602): at dalvik.system.NativeStart.main(Native Method)
09-24 07:59:32.818: E/AndroidRuntime(17602): Caused by: java.lang.NullPointerException
09-24 07:59:32.818: E/AndroidRuntime(17602): at com.abc.cyk.QuestionActivity.processScreen(QuestionActivity.java:41)
09-24 07:59:32.818: E/AndroidRuntime(17602): at com.abc.cyk.QuestionActivity.onCreate(QuestionActivity.java:33)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.app.Activity.performCreate(Activity.java:5104)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-24 07:59:32.818: E/AndroidRuntime(17602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-24 07:59:32.818: E/AndroidRuntime(17602): ... 11 more
09-24 07:59:38.557: I/Process(17602): Sending signal. PID: 17602 SIG: 9
public class QuestionActivity extends Activity implements OnClickListener{
private Question currentQ;
private GamePlay currentGame;
private CountDownTimer counterTimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
processScreen();
}
/**
* Configure current game and get question
*/
private void processScreen()
{
currentGame = ((CYKApplication)getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion();
Button nextBtn1 = (Button) findViewById(R.id.answer1);
nextBtn1.setOnClickListener(this);
Button nextBtn2 = (Button) findViewById(R.id.answer2);
nextBtn2.setOnClickListener(this);
Button nextBtn3 = (Button) findViewById(R.id.answer3);
nextBtn3.setOnClickListener(this);
Button nextBtn4 = (Button) findViewById(R.id.answer4);
nextBtn4.setOnClickListener(this);
Button nextBtn5 = (Button) findViewById(R.id.answer5);
nextBtn5.setOnClickListener(this);
/**
* Update the question and answer options..
*/
setQuestions();
}
/**
* Method to set the text for the question and answers from the current games
* current question
*/
private void setQuestions() {
//set the question text from current question
String question = Utility.capitalise(currentQ.getQuestion());
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
TextView option1 = (TextView) findViewById(R.id.answer1);
option1.setText(Utility.capitalise(answers.get(0)));
TextView option2 = (TextView) findViewById(R.id.answer2);
option2.setText(Utility.capitalise(answers.get(1)));
TextView option3 = (TextView) findViewById(R.id.answer3);
option3.setText(Utility.capitalise(answers.get(2)));
TextView option4 = (TextView) findViewById(R.id.answer4);
option4.setText(Utility.capitalise(answers.get(3)));
int score = currentGame.getScore();
String scr = String.valueOf(score);
TextView score1 = (TextView) findViewById(R.id.score);
score1.setText(scr);
counterTimer=new CountDownTimer(15000, 1000) {
public void onFinish() {
if(currentGame.getRound()==20)
System.exit(0);
currentGame.decrementScore();
processScreen();
}
public void onTick(long millisUntilFinished) {
TextView time = (TextView) findViewById(R.id.timers);
time.setText( ""+millisUntilFinished/1000);
}
};
counterTimer.start();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onClick(View arg0) {
//Log.d("Questions", "Moving to next question");
if(arg0.getId()==R.id.answer5)
{
new AlertDialog.Builder(this)
.setMessage("Are you sure?")
.setCancelable(true)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
}
}).setNegativeButton("No", null).show();
}
else
{
if(!checkAnswer(arg0)) return;
/**
* check if end of game
*/
if (currentGame.isGameOver()){
//Log.d("Questions", "End of game! lets add up the scores..");
//Log.d("Questions", "Questions Correct: " + currentGame.getRight());
//Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
Intent i = new Intent(this, EndgameActivity.class);
startActivity(i);
finish();
}
else
{
Intent i = new Intent(this, QuestionActivity.class);
finish();
startActivity(i);
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK :
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Check if a checkbox has been selected, and if it
* has then check if its correct and update gamescore
*/
private boolean checkAnswer(View v) {
final Button b = (Button) v;
String answer = b.getText().toString();
counterTimer.cancel();
b.setBackgroundResource(R.drawable.ans);
b.setEnabled(false);
//Log.d("Questions", "Valid Checkbox selection made - check if correct");
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
b.setBackgroundResource(R.drawable.ansgreen);
//Log.d("Questions", "Correct Answer!");
currentGame.incrementScore();
}
else{
b.setBackgroundResource(R.drawable.ansred);
//Log.d("Questions", "Incorrect Answer!");
currentGame.decrementScore1();
}
return true;
}
}
startActivity(new Intent(this, QuestionnActivity.class));
startActivity(new Intent(LoActivity.this, QuestionnActivity.class));