根据android中选中的单选按钮打开一个新活动

时间:2016-03-16 19:04:18

标签: android

我正在android上创建一个项目。我仍然是Android应用程序开发的初学者。我有一个go按钮,点击后,会根据选中的单选按钮将我引导到新活动。我有5个单选按钮,选择一个并按下go按钮后,它应该转到新的活动。 问题是,当我在手机上加载应用程序时,当我点击其中一个选项时单选按钮正在显示,但是当我点击进去时,没有任何反应。代码如下:

JAVA代码(India.java)

package com.example.android.travelindia;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class India extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_india);
        this.finish();
       final RadioButton beach = (RadioButton) findViewById(R.id.radio1);
       final RadioButton hillstation = (RadioButton) findViewById(R.id.radio2);
       final RadioButton wildlife = (RadioButton) findViewById(R.id.radio3);
       final RadioButton historical = (RadioButton) findViewById(R.id.radio4);
       final RadioButton reli = (RadioButton) findViewById(R.id.radio5);

       final Button go = (Button) findViewById(R.id.button1);
        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (beach.isChecked()) {
                    Intent Intents= new Intent(India.this, sands.class); // <----- START "BEACHES" ACTIVITY
                    startActivity(Intents);
                    setContentView(R.layout.activity_sands);
                    }
                else if (hillstation.isChecked()) {
                        Intent Intentm = new Intent(getApplicationContext(), mountains.class); // <----- START "HIll STATIONS" ACTIVITY
                        startActivityForResult(Intentm, 0);
                    }
                else if (wildlife.isChecked()) {
                        Intent Intentw = new Intent(getApplicationContext(), forests.class); // <----- START "WILDLIFE" ACTIVITY
                        startActivityForResult(Intentw, 0);
                    }
                else if (historical.isChecked()) {
                        Intent Intenth = new Intent(getApplicationContext(), history.class); // <----- START "HISTORICAL MONUMENTS" ACTIVITY
                        startActivityForResult(Intenth, 0);
                    }
                else if (reli.isChecked()) {
                        Intent Intentr = new Intent(getApplicationContext(), religious.class); // <----- START "RELIGIOUS LOCATIONS" ACTIVITY
                        startActivityForResult(Intentr, 0);
                    }
                }
            });

       }
}

1 个答案:

答案 0 :(得分:1)

你的问题出在你打电话的onCreate(Bundle)上:

 this.finish();
在您setContentView(R.layout.activity_india)之后

告诉当前活动完成。

所以放弃这一行,一切都应该按预期工作。