简体   繁体   中英

Change textview in button error

I'm making a small quiz (5 questions) where the user gets 1 to 5 points on a question. There are 5 answers per question so 5 buttons.

I don't want 5 different activities so I thought about changing the textview in buttons and the question when clicked on an answer/button. I tried this but it gives me an error I can't figure out why.

Anyone got an idea why I got this error? Also, is there a better solution to my problem (dont want 5 activities)? This seems like the easiest solution for a first time android programmer.

Maincode:

 public class Vraag1 extends Activity implements OnClickListener{

 Button a;
 Button b;
 Button c;
 Button d;
 Button e; 
 TextView scoreatm; 
 int int1 = 0;
 int int2 = 0;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_vraag1);

        Button a = (Button) findViewById(R.id.button1);
        a.setOnClickListener(this);

        Button b = (Button) findViewById(R.id.button2);
        b.setOnClickListener(this);

        Button c = (Button) findViewById(R.id.button3);
        c.setOnClickListener(this);

        Button d = (Button) findViewById(R.id.button4);
        d.setOnClickListener(this);

        Button e = (Button) findViewById(R.id.button5);
        e.setOnClickListener(this);  

}

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

      switch (v.getId()) {
         case R.id.button1: 

                int1 = 1;               
                a.setText("Change in this");

          break;
         case R.id.button2:

                int1 = 2;

          break;
         case R.id.button3:

                int1 = 3;

          break;
         case R.id.button4:

                int1 = 4;

          break;
         case R.id.button5:

                int1 = 5;

          break;

      }
   }

XML:

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="De hele tijd"  
android:background="@drawable/yellow1" 
style="@style/ButtonText1"  
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp" >
</Button>

Error logcat:

11-14 11:04:02.745: E/AndroidRuntime(785): FATAL EXCEPTION: main
11-14 11:04:02.745: E/AndroidRuntime(785): java.lang.NullPointerException
11-14 11:04:02.745: E/AndroidRuntime(785):  at com.example.airassist.Vraag1.onClick(Vraag1.java:50)
11-14 11:04:02.745: E/AndroidRuntime(785):  at android.view.View.performClick(View.java:4240)
11-14 11:04:02.745: E/AndroidRuntime(785):  at android.view.View$PerformClick.run(View.java:17721)
11-14 11:04:02.745: E/AndroidRuntime(785):  at android.os.Handler.handleCallback(Handler.java:730)
11-14 11:04:02.745: E/AndroidRuntime(785):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-14 11:04:02.745: E/AndroidRuntime(785):  at android.os.Looper.loop(Looper.java:137)
11-14 11:04:02.745: E/AndroidRuntime(785):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-14 11:04:02.745: E/AndroidRuntime(785):  at java.lang.reflect.Method.invokeNative(Native Method)
11-14 11:04:02.745: E/AndroidRuntime(785):  at java.lang.reflect.Method.invoke(Method.java:525)
11-14 11:04:02.745: E/AndroidRuntime(785):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-14 11:04:02.745: E/AndroidRuntime(785):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-14 11:04:02.745: E/AndroidRuntime(785):  at dalvik.system.NativeStart.main(Native Method

You need to add all 5 button items to layout_vraag1.xml. Right now it is trying to inflate them, they don't exist, and then has a NullPointerException when trying to set attributes on them.

ouch damn.... i see right now!

Remove all "Button" class name from onCreate function...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_vraag1);

    a = (Button) findViewById(R.id.button1);
    a.setOnClickListener(this);

    b = (Button) findViewById(R.id.button2);
    b.setOnClickListener(this);

    c = (Button) findViewById(R.id.button3);
    c.setOnClickListener(this);

    d = (Button) findViewById(R.id.button4);
    d.setOnClickListener(this);

    e = (Button) findViewById(R.id.button5);
    e.setOnClickListener(this);  

}

With Button a = (Button) findViewById(R.id.button1); you create another variable named A that it's not the same A you created above that it's owned by the class. So when you enter the onClick function the first A (the one owned by the class) it's not initialised.

The problem is that you do not initialize your buttons at start, so you get into trouble with the onClick()-method. So initialize them with NULL . I can recommend doing that with any of your member-variables!

do it like:

private Button a = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_vraag1);

    a = (Button) findViewById(R.id.button1);
    a.setOnClickListener(this); 
}

public void onClick(View v) {

  switch (v.getId()) {
     case R.id.button1: 

            int1 = 1;               
            a.setText("Change in this");

      break;
}

Further information about declaration&initialization: http://java.about.com/od/understandingdatatypes/a/declaringvars.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM