-3

While i'm debugging the code it shows no errors ... but while running the app it shows "Unfortunately app has stopped".this is my Activity 2 page... from this page while clicking next button it should move to Activity 3 page.this is my Activity 2 page

                     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;
     }
      } 
     }

========================================== Please check the above code and help me in this issue.. Thanks in advance. and im getting this message in logcat while click button like java.lang.NullPointerException: Attempt to invoke virtual method

vinukariyad
  • 37
  • 11
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – almightyGOSU Jun 30 '15 at 05:37
  • 'btn' where it is defined in Activity2 ?? please check .. – Android Killer Jun 30 '15 at 05:40
  • @ Android Killer .. i had declared 'btn' in activity but the same error – vinukariyad Jun 30 '15 at 05:55
  • @nag my log cat shows:Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.admin.androidapp.Activity2.onClick(Activity2.java:52) – vinukariyad Jun 30 '15 at 05:57
  • you have intialize btn by using findViewById(-). – Nagaraju V Jun 30 '15 at 06:01

1 Answers1

1

The issue is you are calling onclick for btn outside of oncreate. Move the following code inside your onCreate. Also You need to initialise button

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();
                        }


                    }

                });

EDIT:

Try this

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

                        }

                    });

You don't have initialise intent so it always null so it enters the else condition. And you add Toast there.

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

It is showing.

Amsheer
  • 7,046
  • 8
  • 47
  • 81