Unfortunately , appname has Stopped

81 views Asked by At

I am Trying to make a simple android program that starts the mainActivity and when the user clicks on the button the secondActivity Opens and shows a radio Group with three radio Buttons. When the user clicks on the radio button and press the button the string passed to the mainActivity and Should show on the textView On The mainActivity. But When I am Pressing the button of Main Activity To Open SecondActivity. The Error Message Shows Unfortunately, radioButtons has stopped. I have Updated The Manifest Also..

Here is my code:

mainActivity.java

package com.example.radiobuttons;

import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;


public class MainActivity extends Activity  {

    TextView txt1;
    Button b1;
    int requestCode = 12;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Link();
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this , secondActivity.class);
                startActivityForResult(in, requestCode);
            }
        });

    }

    public void Link()
    {
        txt1 = (TextView) findViewById(R.id.txtView1);
        b1 = (Button) findViewById(R.id.Button1);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if ( resultCode == 110){
        String txt = "Your Answer Is " +data.getStringExtra("answer");

        }

    }





}

secondActivity.java

package com.example.radiobuttons;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class secondActivity extends Activity {

    RadioGroup rg;
    Button b1;
    String ans;
    Intent in;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Link();

        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub

                switch (group.getId())
                {
                case R.id.radio0:
                    ans = "Manmohan Singh";

                    break;
                case R.id.radio1:
                    ans = "Mulayam Singh";
                    break;
                case R.id.radio2:
                    ans = "Narender Modi";
                    break;
                }

            }
        });

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                in = new Intent();
                in.putExtra("answer", ans);
                setResult(110,in);
                finish();
            }
        });
    }

    public void Link()
    {
        rg = (RadioGroup) findViewById(R.id.radioGroup1);
        b1 = (Button) findViewById(R.id.Button1);

    }
}

radioButtons Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radiobuttons"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".secondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.radiobutton.SECONDACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

LogCat:

06-20 12:44:05.319: E/AndroidRuntime(1905): FATAL EXCEPTION: main
06-20 12:44:05.319: E/AndroidRuntime(1905): Process: com.example.radiobuttons, PID: 1905
06-20 12:44:05.319: E/AndroidRuntime(1905): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radiobuttons/com.example.radiobuttons.secondActivity}: java.lang.NullPointerException
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.os.Looper.loop(Looper.java:136)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at java.lang.reflect.Method.invoke(Method.java:515)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at dalvik.system.NativeStart.main(Native Method)
06-20 12:44:05.319: E/AndroidRuntime(1905): Caused by: java.lang.NullPointerException
06-20 12:44:05.319: E/AndroidRuntime(1905):     at com.example.radiobuttons.secondActivity.onCreate(secondActivity.java:47)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.app.Activity.performCreate(Activity.java:5231)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-20 12:44:05.319: E/AndroidRuntime(1905):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-20 12:44:05.319: E/AndroidRuntime(1905):     ... 11 more

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.radiobuttons.MainActivity" >

    <TextView
        android:id="@+id/txtView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/Button1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:gravity="center_horizontal"
        android:layout_below="@+id/txtView1"
        android:text="Get Input From Second Activity" />

</RelativeLayout>

second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:text="Prime Minister Of India?" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="21dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="Manmohan Singh" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mulayam Singh" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Narender Modi" />
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="39dp"
        android:text="Submit Answer" />

</RelativeLayout>
2

There are 2 answers

0
Pooja On BEST ANSWER

you get null pointer exception because you are setting wrong id. In your xml you have given android:id="@+id/button1" but you are using Button1 in Link().

0
mattfred On

In your Link() method change the button initialization to:

b1 = (Button) findViewById(R.id.button1);