具有将计算用户的分数但系统崩溃的多选游戏



我有一个示例android应用程序,它将使用单选按钮显示coutry和多选标志,每次用户检查其中一个答案时,系统都会检查是否为真,并使用从一个活动传递到另一个活动的意图在其分数上加25分,传输数据意味着分数,直到用户播放4次,直到系统结束展示他的得分。

到目前为止,我做了将数据传递给第二个的MainActivity,但问题是系统强制关闭,并在日志猫中显示:

有人能帮我解决这个问题吗????

logcat

11-27 22:15:53.609: E/AndroidRuntime(4834): Caused by: java.lang.NullPointerException
11-27 22:15:53.609: E/AndroidRuntime(4834):     at com.devleb.flagology.SecondActivity.onCreate(SecondActivity.java:56)

主活动.java

package com.devleb.flagology;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
    Button btnS;
    RadioGroup rdgS;
    RadioButton rdS1, rdS2, rdS3, rdS4;
    private int score =0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rdgS = (RadioGroup) findViewById(R.id.rdg1);
        rdgS.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if (rdS1.isChecked() || rdS2.isChecked() || rdS3.isChecked()
                        || rdS4.isChecked()) {
                    btnS.setVisibility(View.VISIBLE);
                }
            }
        });
        rdS1 = (RadioButton) findViewById(R.id.rd_s1);
        rdS2 = (RadioButton) findViewById(R.id.rd_s2);
        rdS3 = (RadioButton) findViewById(R.id.rd_s3);
        rdS4 = (RadioButton) findViewById(R.id.rd_s4);
        btnS = (Button) findViewById(R.id.btn_s);
        btnS.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                switch (rdgS.getCheckedRadioButtonId()) {
                case R.id.rd_s1:
                    i.putExtra("score", score);
                    break;
                case R.id.rd_s2:
                    i.putExtra("score", score);
                    break;
                case R.id.rd_s3:
                    i.putExtra("score", score + 25);
                    break;
                case R.id.rd_s4:
                    i.putExtra("score", score);
                    break;
                default:
                    break;
                }
                startActivity(i);
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.hint_icon:
            ShowAlertDialog();
            break;
        case R.id.about_icon:
            Toast.makeText(this, "developed by Georges Matta",
                    Toast.LENGTH_SHORT).show();
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
    public void ShowAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Hint")
                .setMessage("The famouse sport is Bullfighting")
                .setCancelable(false)
                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

秒动.java

package com.devleb.flagology;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class SecondActivity extends Activity {
    Button btnI;
    RadioGroup rdgI;
    RadioButton rdI1, rdI2, rdI3, rdI4;
    // TextView txt_result;
    private int score = 0;
    int finalScore = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            int value = extras.getInt("score");
            finalScore = score + value;
            rdgI = (RadioGroup) findViewById(R.id.rdg2);
            rdgI.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub
                    if (rdI1.isChecked() || rdI2.isChecked()
                            || rdI3.isChecked() || rdI4.isChecked()) {
                        btnI.setVisibility(View.VISIBLE);
                    }
                }
            });
            rdI1 = (RadioButton) findViewById(R.id.rd_I1);
            rdI2 = (RadioButton) findViewById(R.id.rd_I2);
            rdI3 = (RadioButton) findViewById(R.id.rd_I3);
            rdI4 = (RadioButton) findViewById(R.id.rd_I4);
            btnI = (Button) findViewById(R.id.btn_s);
            btnI.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(SecondActivity.this,
                            ThirdActivity.class);
                    switch (rdgI.getCheckedRadioButtonId()) {
                    case R.id.rd_I1:
                        i.putExtra("score", finalScore);
                        break;
                    case R.id.rd_I2:
                        i.putExtra("score", finalScore);
                        break;
                    case R.id.rd_I3:
                        i.putExtra("score", finalScore);
                        break;
                    case R.id.rd_I4:
                        i.putExtra("score", finalScore);
                        break;
                    default:
                        break;
                    }
                    startActivity(i);
                }
            });
                        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }
}

ThirdActivity.java

package com.devleb.flagology;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class ThirdActivity extends Activity {
    TextView txt_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        txt_result = (TextView) findViewById(R.id.txtResult);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String value = String.valueOf(extras.getInt("score"));
            txt_result.setText(value);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.third, menu);
        return true;
    }
}

我大胆猜测您的问题来自以下行:

    btnS = (Button) findViewById(R.id.btn_s);

很可能您对主活动布局(R.layout.activity_main)进行了更改,并从中重命名或删除了<Button>元素。如果我的猜测是正确的,那么这一行将无法在膨胀的布局中找到视图,并且您的btnS对象将为null,从而导致您看到的实际异常。

相关内容

  • 没有找到相关文章

最新更新