点击"右键"按钮,应用停止

时间:2015-05-21 18:19:36

标签: android android-activity

我开始一个活动(通过Intent,我给它putExtra(String):RED,YELLOW,GREEN或BLUE)

currentColor具有我不想点击的颜色。

我有4个按钮(我从源代码中删除了它,因为我认为你不需要它)。

public class ColorChooser extends Activity implements android.view.View.OnClickListener {
String currentColor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Choose a color");
    setContentView(R.layout.color_chooser);
    setFinishOnTouchOutside(false);

    currentColor = getIntent().getExtras().getString("CurrentColor");
}

@Override
public void onBackPressed() {
}

@Override
public void onClick(View v) {

    Intent returnIntent = new Intent();
    String picked = "";

    boolean ok = true;
    while(ok) {
        switch (v.getId()) {
            case R.id.btnRed:
                picked = "RED";
                break;
            case R.id.btnYellow:
                picked = "YELLOW";
                break;
            case R.id.btnGreen:
                picked = "GREEN";
                break;
            case R.id.btnBlue:
                picked = "BLUE";
                break;
        }
        if(picked.equals(currentColor)){
            Toast.makeText(getApplicationContext(), "Choose other color please", Toast.LENGTH_LONG).show(); 
        } else {
            returnIntent.putExtra("Color", picked);
            ok = false;
        }
    }
    setResult(RESULT_OK, returnIntent);
    finish();
}

}

在我的清单中我有这个。所以它显示为对话。我必须选择一种颜色。

<activity
            android:name=".ColorChooser"
            android:theme="@android:style/Theme.Dialog"
            />

我现在的问题是:如果我选择的颜色是!= currentColor,那很好。但是,如果我选择与currentColor相同的颜色,应用程序将停止。应用程序中的按钮显示为单击(黄色)。 我认为是因为我曾经使用过。我不知道怎么做到这一点,我应该选择一种颜色,直到它为止!= currentColor

05-21 18:33:09.782    1873-1873/com.example.thm_wip1.uno E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
            at com.example.thm_wip1.uno.Deck.<init>(Deck.java:25)
            at com.example.thm_wip1.uno.MultiplayerActivity.onCreate(MultiplayerActivity.java:52)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
            at com.example.thm_wip1.uno.CardValue.<clinit>(CardValue.java:13)
            at com.example.thm_wip1.uno.Deck.<init>(Deck.java:25)
            at com.example.thm_wip1.uno.MultiplayerActivity.onCreate(MultiplayerActivity.java:52)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

更改您的onClick,如下所示。从onClick方法中删除while循环。

login = LoginPage.new(browser)
login.enter_email("my_email@example.com")