Button and onClickListner help needed

Put your problem here if it does not fit any of the other categories.

Button and onClickListner help needed

Postby la3bna » Fri Apr 01, 2011 8:23 am

Hi

So I want a button to show the next layout and Im looking through the resource on Android.com but there is something wrong.

Might be me but anyways.

Here is the code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.markel.NSF;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8.  
  9.  
  10. public class MerkerActivity extends Activity  {
  11.  
  12.         public void onCreate(Bundle savedInstanceState) {
  13.             super.onCreate(savedInstanceState);
  14.             setContentView(R.layout.alder);
  15.         }
  16.                 // Button for bever merker
  17.                 Button bever = (Button)findViewById(R.id.bever);
  18.                 button.setOnClickListener(R.id.bever);
  19.                
  20.                
  21.             // Implement the OnClickListener callback
  22.             public void onClick(View v) {
  23.                 setContentView(R.layout.bmerk1);
  24.             }
  25.          
  26.        
  27. }
  28.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


No matter what I do I cant seem to get the button.setOnClickListner to work. I have tried using this as token.

I would like some help to sort this out..
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Top

Re: Button and onClickListner help needed

Postby vhalexxs » Fri Apr 01, 2011 8:28 am

Hi la3bna,

You must implement View.OnClickListener then change the line button.setOnClickListener(R.id.bever) to button.setOnClickListener(this);

Best,
Alex Escobar
Senior Android Engineer
-----------------------
Airpush.com: The Push Notification Ad Network
User avatar
vhalexxs
Developer
Developer
 
Posts: 28
Joined: Mon Mar 14, 2011 3:19 am

Re: Button and onClickListner help needed

Postby la3bna » Fri Apr 01, 2011 10:17 am

Ok I still get the same error.

Syntax error on token, misplaced construct
syntax error on token (this), delete token.

This is what Eclips tells me, even after code update.

Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. public class MerkerActivity extends Activity implements  OnClickListener {
  2.  
  3.         public void onCreate(Bundle savedInstanceState) {
  4.             super.onCreate(savedInstanceState);
  5.             setContentView(R.layout.alder);
  6.         }
  7.                 // Button for bever merker
  8.                 Button bever = (Button)findViewById(R.id.bever);
  9.                 button.setOnClickListener(this);
  10.                
  11.                
  12.             // Implement the OnClickListener callback
  13.             public void onClick(View v) {
  14.                 setContentView(R.layout.bmerk1);
  15.             }
  16.          
  17.        
  18. }
  19.  
Parsed in 0.009 seconds, using GeSHi 1.0.8.4
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Re: Button and onClickListner help needed

Postby la3bna » Fri Apr 01, 2011 10:57 am

Do I need to separate my button function and layout view in two files?

Is it any help that the buttons are image buttons? Is that different?
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Re: Button and onClickListner help needed

Postby la3bna » Mon Apr 04, 2011 10:35 am

Still nothing.
I have searched a lot (thanks you Google ;) )
This is my best yet (but still noting..)
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. package com.markel.NSF;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class Merker extends Activity
  10. {
  11.   public void onCreate(Bundle icicle)
  12.    {
  13.       super.onCreate(icicle);
  14.       setContentView(R.layout.alder);
  15.       Button bever = (Button) findViewById(R.id.bever);
  16.       bever.setOnClickListener(new View.OnClickListener() {
  17.          public void onClick(View v) {
  18.          Intent i = new Intent(Merker.this, Bmerk1Activity.class);
  19.          startActivity(i);
  20.          }
  21.       });
  22.    }
  23. }
  24.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Re: Button and onClickListner help needed

Postby davelis828 » Mon Apr 04, 2011 7:32 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.markel.NSF;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class Merker extends Activity implements OnClickListener
  10. {
  11.   public void onCreate(Bundle icicle)
  12.    {
  13.       super.onCreate(icicle);
  14.       setContentView(R.layout.alder);
  15.       Button bever = (Button) findViewById(R.id.bever);
  16.       bever.setOnClickListener(new View.OnClickListener(this)
  17.    }
  18.    public void onClick(View v){
  19.       if (v==bever){ //your code here}}
  20.  
  21. }
  22.  
  23. Parsed in 0.009 seconds, using GeSHi 1.0.8.4
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
davelis828
Freshman
Freshman
 
Posts: 2
Joined: Sat Apr 02, 2011 10:49 pm

Top

Re: Button and onClickListner help needed

Postby la3bna » Tue Apr 05, 2011 6:01 am

Maybe I should mention that this app uses Tab view ?

I found a site that shows nested activity. Thinking I should try that.http://blog.henriklarsentoft.com/2010/07/android-tabactivity-nested-activities/
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 11 guests