Intent Button Won't Work

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Intent Button Won't Work

Postby nbadal » Fri Jun 12, 2009 5:18 am

I've created a simple app that should switch between two activities with an intent, but when I run it it gives the generic crash message, and I'm not sure what to look for in the debugging logs.

Heres the Java of the button activity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.Activity;
  3.  
  4. import android.content.Intent;
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.view.View;
  9.  
  10. import android.view.View.OnClickListener;
  11.  
  12. import android.widget.Button;
  13.  
  14.  
  15.  
  16. public class StartPage extends Activity {
  17.  
  18.    
  19.  
  20.         Button subjectButton = (Button) findViewById(R.id.SubjectButton);
  21.  
  22.        
  23.  
  24.     @Override
  25.  
  26.     public void onCreate(Bundle savedInstanceState) {
  27.  
  28.         super.onCreate(savedInstanceState);
  29.  
  30.         setContentView(R.layout.startpage);
  31.  
  32.         subjectButton.setOnClickListener(new OnClickListener()
  33.  
  34.         {
  35.  
  36.                         @Override
  37.  
  38.                         public void onClick(View v) {
  39.  
  40.                                 openSubjectList(v);                    
  41.  
  42.                         }
  43.  
  44.         });
  45.  
  46.     }
  47.  
  48.  
  49.  
  50.         private void openSubjectList(View view)
  51.  
  52.         {
  53.  
  54.                 Intent i = new Intent(view.getContext(),SubjectList.class);    
  55.  
  56.                 startActivity(i);
  57.  
  58.         }
  59.  
  60.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


I also put the correct code in the manifest and linked the id's in the xml file
Is there something wrong with this or should I post more?
nbadal
Developer
Developer
 
Posts: 30
Joined: Tue May 26, 2009 3:55 pm

Top

Re: Intent Button Won't Work

Postby WarrenFaith » Fri Jun 12, 2009 8:06 am

nbadal wrote:I'm not sure what to look for in the debugging logs.

Look for the last red lines int the LogCat and post it us :)
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby nmc » Fri Jun 12, 2009 8:31 am

findViewById has to be called after the call to setContentView.

So try:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.startpage);
  2. subjectButton = (Button) findViewById(R.id.SubjectButton);
  3. subjectButton.setOnClickListener(new OnClickListener()
  4. ...
  5.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby nbadal » Sun Jun 14, 2009 6:16 am

nmc wrote:findViewById has to be called after the call to setContentView.

So try:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.startpage);
  2. subjectButton = (Button) findViewById(R.id.SubjectButton);
  3. subjectButton.setOnClickListener(new OnClickListener()
  4. ...
  5.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Sorry for the late response, but that worked perfectly thank you!
nbadal
Developer
Developer
 
Posts: 30
Joined: Tue May 26, 2009 3:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests