在alertDialog中使用Intent

时间:2013-04-17 12:27:04

标签: java android if-statement alertdialog

我有以下警告对话框,该对话框由if语句触发:

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

在名为secondActivity的第二个类中,java看起来像:

 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看起来像

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

但是我一直在startActivity(intent)行上遇到错误。 alertdialog显示正确但单击按钮时失败。有什么建议吗?

编辑:错误是:

 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)

1 个答案:

答案 0 :(得分:0)

尝试

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

和第二项活动

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

和 如果x是整数,则将其转换为字符串,方式为x+""String.valueOf(x),或String str = Integer.toString(x);