代码没有错误,但模拟器说app停止了

时间:2015-06-24 09:22:37

标签: java android button

现在问题已经改变了...在调试代码之后它没有显示错误但是当我运行代码模拟器显示“不幸的应用程序已经停止” 请帮我解决这个问题。 提前谢谢。

这是我的主要源代码。      包com.example.admin.androidapp;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.View.OnClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.content.Context;
import android.widget.Toast ;


    public class Welcome extends Activity
    {
        RadioGroup radiogroup;
        Button btn;
        private Intent mIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);

                radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int RadioID) {

                    RadioButton rd = (RadioButton)radioGroup.findViewById(RadioID);

                switch (rd.getId())
                {
                    case R.id.radioButton:
                        mIntent = new Intent(Welcome.this, Terminate.class);
                        break;

                    case R.id.radioButton2:
                        mIntent = new Intent(Welcome.this, Activity2.class);
                        break;
                }
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mIntent != null){
                    startActivity(mIntent);}
                else {
                    Toast.makeText(getApplicationContext(), "No choice made yet", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个:

修改

public class Welcome extends Activity
{
    RadioGroup radiogroup;
    Button btn;
    private Intent mIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
        btn = (Button)findViewById(R.id.nextButton);

        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int RadioID) {

                RadioButton rd = (RadioButton)radioGroup.findViewById(RadioID);

                switch (rd.getId())
                {
                    case R.id.radiobutton:
                        mIntent = new Intent(Welcome.this, Terminate.class);
                        break;

                    case R.id.radiobutton2:
                        mIntent = new Intent(Welcome.this, Activity2.class);
                        break;
                }
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mIntent != null){
                    startActivity(mIntent);}
                else {
                    Toast.makeText(getApplicationContext(), "No choice made yet", Toast.LENGTH_SHORT).show();
                }      
             }
         });
    }
}

希望这会帮助你;)

答案 1 :(得分:0)

您应该尝试这样

RadioGroup group=(RadioGroup) findViewById(R.id.radioGroup1);

    group.setOnCheckedChangeListener(new OnCheckedChangeListener() 
       {

        public void onCheckedChanged(RadioGroup group, int checkedId) 
           {
            // TODO Auto-generated method stub
            if(radiobutton1.isChecked())
              {

                        startActivity(new Intent(Welcome.this, Terminate.class));
              }
            else if(radiobutton2.isChecked())
              {

               startActivity(new Intent(Welcome.this, Activity2.class));
              }
         }
    });