模拟器显示“遗憾的是应用已经停止”

时间:2015-06-30 05:35:55

标签: java android android-studio

当我正在调试代码时,它显示没有错误...但是在运行应用程序时它显示“遗憾的应用程序已停止”。这是我的活动2页面...从此页面点击下一个按钮它应该移动到活动3页面。这是我的活动2页面

                     package com.example.admin.androidapp;

        import android.app.Activity;
        import android.content.Context;
        import android.content.Intent;
        import android.os.Bundle;
        import android.widget.Button;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Toast;

        public class Activity2 extends Activity  {

            Button btn11;
            Button btn22;
            public Intent intent;
            Button btn;



            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_activity2);

                btn11 = (Button) findViewById(R.id.button2);
                btn22 = (Button) findViewById(R.id.button5);

                 btn = (Button) findViewById(R.id.button2);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {
                            case R.id.button2:
                                intent = new Intent(Activity2.this, Activity3.class);
                                startActivity(intent);
                                break;

                            case R.id.button5:
                                intent= new Intent(Activity2.this, Welcome.class);
                                startActivity(intent);
                                break;


                        }
                    }

                });

            }





        }

===============================================================

here is my Activity3 page:


    package com.example.admin.androidapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

    public   class Activity3 extends Activity{
    RadioGroup radiogroup;
    Button btn1;
    private Intent mIntent;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity3);

        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
        btn1 = (Button) findViewById(R.id.button);

        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.radioButton7:
                       mIntent = new Intent(Activity3.this, Terminate.class);
                        break;
    case R.id.radioButton8:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton10:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton11:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton15:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton16:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton17:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton18:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton19:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton13:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
    case R.id.radioButton12:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
      case R.id.radioButton14:
                    mIntent = new Intent(Activity3.this, Activity4.class);
                    break;
     }
      } 
     }

========================================== 请检查上面的代码,并在这个问题上帮助我.. 提前致谢。 我在logcat中获取此消息,而单击按钮如java.lang.NullPointerException:尝试调用虚方法

1 个答案:

答案 0 :(得分:1)

问题是你在oncreate之外的btn上调用onclick。在onCreate中移动以下代码。您还需要initialise按钮

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


                    }

                });

修改

试试这个

btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                           Intent intent = new Intent(Activity2.this, Activity3.class);
    startActivity(intent);

                        }

                    });

您没有初始化意图,因此它始终为null,因此它进入else条件。你在那里添加Toast。

Toast.makeText(getApplicationContext(), "No choice made yet", Toast.LENGTH_SHORT).show(); 

正在展示。