Unfortunately , appname has Stopped

Akshay

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>
Pooja

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().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unfortunately appName has stopped error

From Dev

Android Project in Eclipse, Unfortunately 'appName' Has Stopped

From Dev

App stops on button click with error message "Appname has stopped unfortunately"

From Dev

Difference between Application Not Responding and Unfortunately <Appname> has stopped in Android?

From Dev

Unfortunately MyFirstApp has stopped

From Dev

Unfortunately, "application" has stopped

From Dev

unfortunately, MyOwnSQLite has stopped

From Dev

Unfortunately App Has Stopped

From Dev

unfortunately button has stopped

From Dev

OnClickListner unfortunately has stopped

From Dev

Phonegap 3.2.0 "Unfortunately, AppName has stopped." dialog when closing app

From Dev

Starting my very first app in the emulator results in "unfortunately, AppName has stopped"

From Dev

Unfortunately, app has stopped working?

From Dev

Unfortunately, <project name> has stopped

From Dev

Android : Unfortunately, app has stopped

From Dev

Unfortunately,"App" has stopped, OnClickListener

From Dev

Unfortunately IrrHelloWorld has stopped - Android

From Dev

Unfortunately, app has stopped working?

From Dev

Unfortunately, (application name) has stopped

From Dev

Unfortunately,"App" has stopped, OnClickListener

From Dev

unfortunately,myapp has stopped working

From Dev

Unfortunately the app has stopped working

From Dev

Alternative of 'unfortunately app has stopped'

From Dev

My app has unfortunately stopped

From Dev

android app unfortunately has stopped

From Dev

Android : Unfortunately, MyApp has stopped

From Dev

Unfortunately, my application has stopped

From Dev

Unfortunately Google play services has stopped in emulator

From Dev

Unfortunately my app has stopped in Android Emulator

Related Related

  1. 1

    Unfortunately appName has stopped error

  2. 2

    Android Project in Eclipse, Unfortunately 'appName' Has Stopped

  3. 3

    App stops on button click with error message "Appname has stopped unfortunately"

  4. 4

    Difference between Application Not Responding and Unfortunately <Appname> has stopped in Android?

  5. 5

    Unfortunately MyFirstApp has stopped

  6. 6

    Unfortunately, "application" has stopped

  7. 7

    unfortunately, MyOwnSQLite has stopped

  8. 8

    Unfortunately App Has Stopped

  9. 9

    unfortunately button has stopped

  10. 10

    OnClickListner unfortunately has stopped

  11. 11

    Phonegap 3.2.0 "Unfortunately, AppName has stopped." dialog when closing app

  12. 12

    Starting my very first app in the emulator results in "unfortunately, AppName has stopped"

  13. 13

    Unfortunately, app has stopped working?

  14. 14

    Unfortunately, <project name> has stopped

  15. 15

    Android : Unfortunately, app has stopped

  16. 16

    Unfortunately,"App" has stopped, OnClickListener

  17. 17

    Unfortunately IrrHelloWorld has stopped - Android

  18. 18

    Unfortunately, app has stopped working?

  19. 19

    Unfortunately, (application name) has stopped

  20. 20

    Unfortunately,"App" has stopped, OnClickListener

  21. 21

    unfortunately,myapp has stopped working

  22. 22

    Unfortunately the app has stopped working

  23. 23

    Alternative of 'unfortunately app has stopped'

  24. 24

    My app has unfortunately stopped

  25. 25

    android app unfortunately has stopped

  26. 26

    Android : Unfortunately, MyApp has stopped

  27. 27

    Unfortunately, my application has stopped

  28. 28

    Unfortunately Google play services has stopped in emulator

  29. 29

    Unfortunately my app has stopped in Android Emulator

HotTag

Archive