-1

am new to android ,I juz wanna know how to make like when i click button1, then button2 the text view will show 1 2,,, but if I clicked button2 then button1 it will show 2 1

this is my codes but now when I click button1 then button2, or if i clicked button2 then button1 it shows the same,, plz help me thanx.

now I did it using the getText & setText its working as I wanted but the condition if its correct or not is not working plz help !! what is wrong with it and is my method correct?

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // Full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.wordgame2);

    bu1 = (Button) findViewById(R.id.bu1);
    bu2 = (Button) findViewById(R.id.bu2);
    bu3 = (Button) findViewById(R.id.bu3);
    bu4 = (Button) findViewById(R.id.bu4);
    bu5 = (Button) findViewById(R.id.bu5);


    t1 = (TextView) findViewById(R.id.t1);

    i1 = (ImageView) findViewById(R.id.i1);
    i2 = (ImageView) findViewById(R.id.i2);

    bu1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            t1.setText(t1.getText()+" "+bu1.getText()); 

        }

    });

    bu2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    t1.setText(t1.getText()+" "+bu2.getText()); 


                }

            });

    bu3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            t1.setText(t1.getText()+" "+bu3.getText()); 

        }

    });

    bu4.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub


            t1.setText(t1.getText()+" "+bu4.getText()); 
        }

    });

    bu5.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        if(t1.getText().toString().equals("a b c d")) //this is not working
            {

                i1.setVisibility(View.VISIBLE);
                i2.setVisibility(View.INVISIBLE);
            }       

            else {
                i2.setVisibility(View.VISIBLE);
                i1.setVisibility(View.INVISIBLE);
            }
        }

    });


}

04-21 07:00:48.690: E/AndroidRuntime(23874): FATAL EXCEPTION: main 04-21 07:00:48.690: E/AndroidRuntime(23874): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arabicalphabets.reham/com.arabicalphabets.reham.Wordgame2}: java.lang.NullPointerException 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.app.ActivityThread.access$600(ActivityThread.java:127) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.os.Handler.dispatchMessage(Handler.java:99) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.os.Looper.loop(Looper.java:137) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.app.ActivityThread.main(ActivityThread.java:4512) 04-21 07:00:48.690: E/AndroidRuntime(23874): at java.lang.reflect.Method.invokeNative(Native Method) 04-21 07:00:48.690: E/AndroidRuntime(23874): at java.lang.reflect.Method.invoke(Method.java:511) 04-21 07:00:48.690: E/AndroidRuntime(23874): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982) 04-21 07:00:48.690: E/AndroidRuntime(23874): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749) 04-21 07:00:48.690: E/AndroidRuntime(23874): at dalvik.system.NativeStart.main(Native Method) 04-21 07:00:48.690: E/AndroidRuntime(23874): Caused by: java.lang.NullPointerException 04-21 07:00:48.690: E/AndroidRuntime(23874): at com.arabicalphabets.reham.Wordgame2.onCreate(Wordgame2.java:36) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.app.Activity.performCreate(Activity.java:4465) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 04-21 07:00:48.690: E/AndroidRuntime(23874): ... 11 more

2 Answers2

0

first instead of declaring each button individually use looping to declare them...

Button[] buttons; 
for(int i=0; i<5; i++) {  //for your 5 buttons
{
 String buttonID = "bu" + (i+1);
 int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
 buttons[i] = ((Button) findViewById(resID));
}

-using switch() find the clicked button...

-and as cases implement the onclick() as below

public void onClick(View v) {
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString()); 
}

hope it works

karan
  • 8,637
  • 3
  • 41
  • 78
  • I dont know y its crash now every time I open it, and still the if else statement which is ( if(t1.getText().toString().equals("a b c d")) its not working when they click bu5 if t1 is correct as "a b c d" it will show picture y this method not working when i had no error? – Riham Luay Niyazi Arnaout Apr 20 '13 at 16:30
0

I cant see what is wrong with it and why its crashing and why the if else statement isn't working...??

public class Wordgame2 extends Activity implements OnClickListener {

Button[] buttons; 
TextView t1;
ImageView i1, i2;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // Full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.wordgame2);

    for(int i=0; i<5; i++) {  
        {
         String buttonID = "bu" + (i+1);
         int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
         buttons[i] = ((Button) findViewById(resID));
         buttons[i].setOnClickListener(this);
        }
        }
    t1 = (TextView) findViewById(R.id.t1);

    i1 = (ImageView) findViewById(R.id.i1);
    i2 = (ImageView) findViewById(R.id.i2);







}
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()){

    case R.id.bu1:
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString());
        break;

    case R.id.bu2:
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString());
        break;

    case R.id.bu3:
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString());
        break;

    case R.id.bu4:
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString());
        break;

//when they click bu5 if t1 is correct as "a b c d" it will show picture

    case R.id.bu5:


        if(t1.getText().toString().equals("a b c d")) //this is not working
        {

            i1.setVisibility(View.VISIBLE);
            i2.setVisibility(View.INVISIBLE);
        }       

        else {
            i2.setVisibility(View.VISIBLE);
            i1.setVisibility(View.INVISIBLE);
        }

        break;

    }
}

}

Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84