0

I have the following alert dialog that is triggered with an if statement:

if (x < 10) {
               AlertDialog.Builder builder= new AlertDialog.Builder(parentActivity);
                builder.setTitle(x.toString());
               builder.setMessage(x.toString());
               builder.setPositiveButton("x", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub//
                    Intent intent = new Intent(parentActivity.getApplicationContext(),secondActivity.class);
                    Bundle b = new Bundle();
                    b.putString("x",x.toString());
                    intent.putExtras(b);

                    startActivity(intent);

                }});

            AlertDialog dialog = builder.create();
            dialog.show();

In the second class, called secondActivity, the java looks like:

 public class secondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xView);
        Button b = (Button) findViewById(R.id.button1);
}
}

Manifest looks like

</activity>
           <activity
        android:name=".secondActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="uk.ac.ucl.cege.ucestw0.SECONDACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

However I keep getting errors on startActivity(intent) line. The alertdialog displays correctly but fails when the button is clicked. Any suggestions?

EDIT: Errors are:

 threadid=1: thread exiting with uncaught exception (group=0x40015560)
    E/AndroidRuntime(1103): FATAL EXCEPTION: main
    E/AndroidRuntime(1103): java.lang.NullPointerException
    E/AndroidRuntime(1103):     at android.app.Activity.startActivityForResult(Activity.java:2827)
    E/AndroidRuntime(1103):     at android.app.Activity.startActivity(Activity.java:2933)
    E/AndroidRuntime(1103):     at my.package.QuizMeLocationListener$1.onClick(mainAcitivity.java:79)
    E/AndroidRuntime(1103):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
    E/AndroidRuntime(1103):     at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime(1103):     at android.os.Looper.loop(Looper.java:130)
    E/AndroidRuntime(1103):     at android.app.ActivityThread.main(ActivityThread.java:3683)
    E/AndroidRuntime(1103):     at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(1103):     at java.lang.reflect.Method.invoke(Method.java:507)
    E/AndroidRuntime(1103):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    E/AndroidRuntime(1103):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    E/AndroidRuntime(1103):     at dalvik.system.NativeStart.main(Native Method)
Shiv
  • 4,431
  • 3
  • 21
  • 38

1 Answers1

0

Try

Intent intent = new Intent(CurrentActivity.this,secondActivity.class);   //CurrentActivity is your current activity name
intent.putExtra("x",x.toString());
startActivity(intent);

and in second activity

  Intent intent = getIntent();
  String x= intent.getStringExtra("x");

and if x is an integer convert it to string this way x+"" or String.valueOf(x), or String str = Integer.toString(x);

Shiv
  • 4,431
  • 3
  • 21
  • 38
  • http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context. getApplicationContext() in not recommended – Raghunandan Apr 17 '13 at 13:00
  • @Shiv Ok thanks. So is CurrentActivity the name of my class or do i literally type CurrentActivity? Sorry for the stupid questions! – user2076033 Apr 17 '13 at 13:17
  • hehe ya current activity is your current class name in which you are working – Shiv Apr 17 '13 at 13:19
  • and if x is an integer convert it like x+"" or String.valueOf(x), or String str = Integer.toString(x); – Shiv Apr 17 '13 at 13:27
  • @Shiv Sorry, still get an error on this line! : Intent intent = new Intent(CurrentActivity.this,secondActivity.class); – user2076033 Apr 17 '13 at 13:42
  • 1
    @user2076033 Instead of CurrentActivity.this use your ActivityName.this (name of your activity class). In your case parentActivity is the name of your ativity class i guess – Raghunandan Apr 17 '13 at 13:45
  • @user2076033- if you are using CurrentActivity as it is you are doing it wrong use your Activity name at the place of CurrentActivity – Shiv Apr 17 '13 at 13:48
  • @Shiv Thanks, I used my activity name but still not luck! – user2076033 Apr 17 '13 at 15:00