same code works the first time but not the second

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

same code works the first time but not the second

Postby trashbag » Wed Feb 22, 2012 4:40 am

Hi,
i have a bit of a problem with a application i am coding, the problem consists of using Intent to switch the paged viewed by the user. the code for each button is similar, with only variables being changed. below is my .java code.
Code: Select all
package com.trashbag.SchoolKit;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;


public class SKIT extends Activity implements OnClickListener {
   private Button MathBtn;
   private Button HistoryBtn;
   private Button LanguagesBtn;
   private Button EnglishBtn;
   private Button ScienceBtn;
   private Button BugBtn;
   private Button DonateBtn;
   private Button SuggestASubjectBtn;   
   
   protected void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      
      MathBtn = (Button) findViewById(R.id.MathBtn);
      HistoryBtn = (Button) findViewById(R.id.HistoryBtn);
      LanguagesBtn = (Button) findViewById(R.id.LanguagesBtn);
      EnglishBtn = (Button) findViewById(R.id.EnglishBtn);
      ScienceBtn = (Button) findViewById(R.id.ScienceBtn);
      BugBtn = (Button) findViewById(R.id.BugBtn);
      DonateBtn = (Button) findViewById(R.id.DonateBtn);
      SuggestASubjectBtn = (Button) findViewById(R.id.SuggestASubject);
      
      MathBtn.setOnClickListener(this);
      HistoryBtn.setOnClickListener(this);
      LanguagesBtn.setOnClickListener(this);
      EnglishBtn.setOnClickListener(this);
      ScienceBtn.setOnClickListener(this);
      BugBtn.setOnClickListener(this);
      DonateBtn.setOnClickListener(this);
      SuggestASubjectBtn.setOnClickListener(this);
      
   }
   public void onClick(View v){
      switch (v.getId()) {
      case R.id.MathBtn:
         Intent mathIntent = new Intent(this,MATH.class);
         this.startActivity(mathIntent);
         break;
      case R.id.HistoryBtn:
         Intent historyIntent = new Intent(this,HISTORY.class);
         this.startActivity(historyIntent);
         break;
      case R.id.LanguagesBtn:
         Intent languagesIntent = new Intent(this,LANGUAGES.class);
         this.startActivity(languagesIntent);
         break;
      case R.id.EnglishBtn:
         Intent englishIntent = new Intent(this,ENGLISH.class);
         this.startActivity(englishIntent);
         break;
      case R.id.ScienceBtn:
         Intent scienceIntent = new Intent(this,SCIENCE.class);
         this.startActivity(scienceIntent);
         break;
      case R.id.BugBtn:
         Intent bugIntent = new Intent(this,BUG.class);
         this.startActivity(bugIntent);
         break;
      case R.id.DonateBtn:
         Intent donateIntent = new Intent(this,DONATE.class);
         this.startActivity(donateIntent);
         break;
      case R.id.SuggestASubject:
         Intent suggestIntent = new Intent(this,SUGGEST.class);
         this.startActivity(suggestIntent);
         break;
         
      }
   }
   }


any help is greatly appreciated.

thank you in advance,

Alex
(trashbag)

*EDIT: sorry if this is in the wrong section*
trashbag
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 21, 2012 10:12 pm

Top

Re: same code works the first time but not the second

Postby Phyll » Wed Feb 22, 2012 5:06 am

Hi trashbag,

And you use finish() to end any of these secondary activities so that you return to this one?

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: same code works the first time but not the second

Postby trashbag » Wed Feb 22, 2012 5:16 am

where do i put it? at the end of the Intent statement? or at the end of the new .java file?
trashbag
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 21, 2012 10:12 pm

Re: same code works the first time but not the second

Postby Phyll » Wed Feb 22, 2012 12:11 pm

Hi trashbag,

For instance, one of your secondary activities might follow this pattern:

Code: Select all
public class MATH extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.mathactivity);
        Button done = (Button)findViewById(R.id.button_done);
        done.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                finish();
                }
            });
        }
    //TODO: Fill In Methods Etc.
    }//class


By the way, all capital letters is usually reserved for constants. Class names are usually a capital letter on the first letter of any whole words but not all caps, like Math or MathActivity. At least that is more conventional.

This would return to your previous SKIT in the same way as pressing the Back key.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: same code works the first time but not the second

Postby trashbag » Wed Feb 22, 2012 8:14 pm

Phyll,

it worked, thank you so much.

trashbag
trashbag
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 21, 2012 10:12 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 8 guests