0

I have 2 activity screens. The 1st one has 2 buttons. Button1 processes the data based from the user inputs while Button2 leads the user to the next activity screen based from the result of the process.

Whenever I click Button1, nothing happens, and

when i click Button2, the app stops working and gives the "Unfortunately..." message. At first, everything is going well but then I added a few codes then the app won't run.

MainActivity

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    //for the header
        View title = getWindow().findViewById(android.R.id.title);
        if (title != null)
        { 
            ViewParent titleBar = title.getParent();
            if (titleBar != null && (titleBar instanceof View))
            { 
                View parentView = (View)titleBar; 
                parentView.setBackgroundColor(Color.RED); 
            }
        }

    //for button2
        Button next = (Button) findViewById(R.id.DGButton);
        next.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myIntent, 0);
            }

        });
    } 

    public void calculateClickHandler(View view)
    {
        //handle the click of button1

        if (view.getId() == R.id.CalculateButton)
        {
         //some codes

    //the next activity screen uses the calculated value for some outputs
         Intent intent1 = new Intent(MainActivity.this, Activity2.class);
        Bundle b = new Bundle();
        b.putDouble("key", bmiValue);
        intent1.putExtras(b);
        startActivityForResult(intent1,0);
    }
    }

Activity2:

public class Activity2 extends Activity
{ 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Button next = (Button) findViewById(R.id.BackToBMI);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });
//some codes

        Bundle b = getIntent().getExtras(); //the nullpointerexception is here
        double bmival = b.getDouble("key");
}
}

Logcat:

12-27 05:15:03.263: E/AndroidRuntime(1091): FATAL EXCEPTION: main
12-27 05:15:03.263: E/AndroidRuntime(1091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.Activity2}: java.lang.NullPointerException
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.os.Looper.loop(Looper.java:137)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at java.lang.reflect.Method.invokeNative(Native Method)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at java.lang.reflect.Method.invoke(Method.java:511)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at dalvik.system.NativeStart.main(Native Method)
12-27 05:15:03.263: E/AndroidRuntime(1091): Caused by: java.lang.NullPointerException
12-27 05:15:03.263: E/AndroidRuntime(1091):     at com.example.bmicaldg.Activity2.onCreate(Activity2.java:33)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.app.Activity.performCreate(Activity.java:5104)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-27 05:15:03.263: E/AndroidRuntime(1091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-27 05:15:03.263: E/AndroidRuntime(1091):     ... 11 more

And also this is the XML file for the MainActivity:

Button1:

<Button
        android:id="@+id/CalculateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView4"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="14dp"
        android:background="#FF0000"
        android:minHeight="30dip"
        android:minWidth="65dip"
        android:onClick="calculateClickHandler"
        android:text="@string/CalculateButton"
        android:textColor="#FFFFFF"
        android:textSize="14sp" />

Button2:

<Button
        android:id="@+id/DGButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="#FF0000"
        android:minHeight="30dip"
        android:onClick="calculateClickHandler"
        android:text="@string/DGButton"
        android:textColor="#FFFFFF" />

I'm a beginner in Android Mobile programming so any help would be appreciated.

April Mosca
  • 35
  • 1
  • 9

5 Answers5

1

try replacing following code:

//for button2
        Button next = (Button) findViewById(R.id.DGButton);
        next.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myIntent, 0);
            }

        });

with this:

//for button2
        Button next = (Button) findViewById(R.id.DGButton);
        next.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Intent myIntent = new Intent(MainActivity.this, Activity2.class);
                startActivityForResult(myIntent, 0);
            }

        });

EDIT1: updated after logcat logs..

in the last line of the method double bmival = b.getDouble("key"); you are trying to read value from the bundle. As per the following javadoc for the method getExtras():

it returns the map of all extras previously added with putExtra(), or null if none have been added.

Now since you are not setting any value in the MainActivity by using putExtra(), you are getting null in the response to the call to getExtras(). So you need to call putExtra() in the MainActivity to pass some value to Activity2 like following code:

//for button2
        Button next = (Button) findViewById(R.id.DGButton);
        next.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                 //Assuming bmival is a class variable that has the value that you want to pass on to Activity2
                  myIntent.putExtra("key",bmival);

                startActivityForResult(myIntent, 0);
            }

        });

Also please put a null check before reading value form the bundle like in the following code:

if(b!=null){
   double bmival = b.getDouble("key");
}
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
  • On my xml file, are the values of the android:onClick buttons the same which is calculateClickHandler? – April Mosca Dec 27 '12 at 06:09
  • check out this post http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene to under stand the `android:onClick` attribute... – Praful Bhatnagar Dec 27 '12 at 06:12
1

Its better to Use startActivity() instead of startActivityForResult(intent1,0)

and also in your receiving side check whether the you are getting extras are not

So Try like this

 Bundle extras = getIntent().getExtras();
 if(extras != null)
 {
    double bmival = extras.getDouble("key");
 }
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
1

This error is occurred because you are not passing double value in intent on click event of next button.

Write below code

Intent intent1 = new Intent(MainActivity.this, Activity2.class);
intent1.putExtra("key", bmiValue);
startActivity(intent1);

instead of below code on click event of next button and intent code of calculateClickHandler() method.

Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);

it will solve your problem.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
1

I think you looking for this, try this

    //for button2
    Button next = (Button) findViewById(R.id.DGButton);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(MainActivity.this, Activity2.class);
            Bundle b = new Bundle();
            b.putDouble("key", bmiValue);
            intent1.putExtras(b);
            startActivityForResult(myIntent, 0);
        }

    });
Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
Charan Pai
  • 2,288
  • 5
  • 32
  • 44
1

There might be few errors,

  • First and the most possible case might be that you have not defined your second Activity in your manifest file. So first check that whether you have defined all of your activities in manifest file.

  • The following statement is completely wrong.

    Intent myIntent = new Intent(view.getContext(), Activity2.class);

Here you are using View of Buttin's onClickListener, but instead you should be using either the Activity Context, like MyActivity.this or the Application's Context, like this getApplicationContext().

  • Try to use

    startActivity(myIntent);

instead of

startActivityForResult(myIntent, 0);
  • If at all your problem is not solved, the last not so possible case might be,

try adding flags to the intent, like this

myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  • You are not setting any values in the Intent Extras, Although it would give you an error in the next Activity,

    myIntent.putExtra("key", bmiValue);

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100