简体   繁体   中英

What's wrong with my OnClickListener?

I have this jokes app on Google Play (Punny Jokes) and to get a joke, the user must tap anywhere on the screen, then on the joke screen, they get to read the joke. But when they want another joke, they must go back to the main screen and press the screen again, which tends be annoying. I'm trying to set up another full-screen button on the joke screen activity, so they don't have to go back. The jokes are in strings in I have code that selects a random random string in a class called "StartingPoint". Thanks so much!

 public class DisplayMessageActivity extends Activity implements OnClickListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

**//ERROR BEGINS HERE**

    Button next;
    Button next = (Button) = findViewById (R.id.next);
    next.setOnClickListener(this);


**//ERROR ENDS HERE**       



    initTypeface1(); 
}

@Override
public void onClick(final View v) {
     switch(v.getId()){
     case R.id.next:
         IntentHandler.switchActivity(DisplayMessageActivity.this,
                StartingPoint.class, false);
                  break;
    // TODO Auto-generated method stub

}
}
 };

You have declared the field next twice. And you had an equal sign in a completely wrong place.

Button next =  (Button) findViewById (R.id.next);
next.setOnClickListener(this);

or

Button next;
next = (Button) findViewById (R.id.next);
next.setOnClickListener(this);

Well you should modify code like this

Button next = (Button) findViewById (R.id.next);

and why do you use next value twice??

and solution is also in this block

//ERROR BEGINS HERE

Button next;
next = (Button) findViewById(R.id.next);
next.setOnClickListener(this);

//ERROR ENDS HERE

do replace your code as above

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