more than one button click event crashes?

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

more than one button click event crashes?

Postby reynolds » Wed Mar 11, 2009 5:58 am

Help guys,

My simple apps crashes immediately when I tried to add two button onclick events. Can you tell my what am I doing wrong here? thanks

EDIT... When I put one(any) of the button setOnClickListener in a comment, thus disabling action for the button , the apps loads normally. I could click on the button and it shows the other view, but I can't go back since no action is set from the other button.

file: demo1.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package mrm.demo1;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8.  
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.widget.Button;
  13.  
  14.  
  15.  
  16. import android.view.View;
  17.  
  18. import android.view.Window;
  19.  
  20. import android.view.WindowManager;
  21.  
  22. import android.view.View.OnClickListener;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. public class demo1 extends Activity {
  29.  
  30.        
  31.  
  32.     /** Called when the activity is first created. */
  33.  
  34.         public int counter;
  35.  
  36.     @Override
  37.  
  38.     public void onCreate(Bundle savedInstanceState) {
  39.  
  40.         super.onCreate(savedInstanceState);
  41.  
  42.         this.setFullscreen();
  43.  
  44.         this.setNoTitle();
  45.  
  46.         setContentView(R.layout.main);
  47.  
  48.        
  49.  
  50.        
  51.  
  52.         Button btnDemo = (Button)findViewById(R.id.btnDemo);
  53.  
  54.         btnDemo.setOnClickListener( new OnClickListener(){
  55.  
  56.                 @Override
  57.  
  58.                         public void onClick(View v)
  59.  
  60.                         {
  61.  
  62.                         setContentView(R.layout.demo1);
  63.  
  64.                        
  65.  
  66.                         }
  67.  
  68.         });
  69.  
  70.  
  71.  
  72.         Button btnMain = (Button)findViewById(R.id.btnMain);
  73.  
  74.         btnMain.setOnClickListener( new OnClickListener(){
  75.  
  76.                 @Override
  77.  
  78.                         public void onClick(View v)
  79.  
  80.                         {
  81.  
  82.                         setContentView(R.layout.main);
  83.  
  84.                        
  85.  
  86.                         }
  87.  
  88.         });
  89.  
  90.  
  91.  
  92.     }
  93.  
  94.     ////////////////////////////////
  95.  
  96.     public void setFullscreen() {
  97.  
  98.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  99.  
  100.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  101.  
  102.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  103.  
  104.     }
  105.  
  106.    ////////////////////////////////////
  107.  
  108.     public void setNoTitle() {
  109.  
  110.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  111.  
  112.     }    
  113.  
  114.    
  115.  
  116.    
  117.  
  118. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


file: demo1.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:layout_width="fill_parent"
  6.  
  7.         android:layout_height="fill_parent">
  8.  
  9.        
  10.  
  11. <TextView  
  12.  
  13.    android:layout_width="fill_parent"
  14.  
  15.    android:layout_height="wrap_content"
  16.  
  17.    android:text="This is the 'demo1.xml' view."
  18.  
  19.    android:layout_gravity="center_horizontal"
  20.  
  21.    />
  22.  
  23.        
  24.  
  25. <Button
  26.  
  27.     android:id="@+id/btnMain"
  28.  
  29.     android:layout_width="wrap_content"
  30.  
  31.     android:layout_height="wrap_content"
  32.  
  33.     android:text="Open MainView"
  34.  
  35.     android:hint="Click to open main view"
  36.  
  37.     android:textSize="10px"
  38.  
  39.     android:layout_gravity="center_horizontal|bottom"
  40.  
  41.    
  42.  
  43.  />
  44.  
  45.  
  46.  
  47. <ImageView
  48.  
  49.     android:layout_width="fill_parent"
  50.  
  51.     android:layout_height="fill_parent"
  52.  
  53.     android:scaleType="center"
  54.  
  55.     android:src="@drawable/property"
  56.  
  57.     />
  58.  
  59.        
  60.  
  61. </FrameLayout>
  62.  
  63.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


file: main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:layout_width="fill_parent"
  7.  
  8.    android:layout_height="fill_parent"
  9.  
  10.    >
  11.  
  12. <TextView  
  13.  
  14.    android:layout_width="fill_parent"
  15.  
  16.    android:layout_height="wrap_content"
  17.  
  18.    android:text="This is the 'main.xml' view."
  19.  
  20.    android:layout_gravity="center_horizontal"
  21.  
  22.    />
  23.  
  24.  
  25.  
  26. <Button
  27.  
  28.     android:id="@+id/btnDemo"
  29.  
  30.     android:layout_width="wrap_content"
  31.  
  32.     android:layout_height="wrap_content"
  33.  
  34.     android:text="Open DemoView"
  35.  
  36.     android:hint="Click to open demo view"
  37.  
  38.     android:textSize="10px"
  39.  
  40.     android:layout_gravity="center_horizontal|bottom"
  41.  
  42.    
  43.  
  44.  />
  45.  
  46.  
  47.  
  48. <ImageView
  49.  
  50.     android:layout_width="fill_parent"
  51.  
  52.     android:layout_height="fill_parent"
  53.  
  54.     android:scaleType="center"
  55.  
  56.     android:src="@drawable/insurance"
  57.  
  58.     />  
  59.  
  60. </FrameLayout>
  61.  
  62.  
  63.  
  64.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


file: AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.      package="mrm.demo1"
  4.  
  5.      android:versionCode="1"
  6.  
  7.      android:versionName="1.0.0">
  8.  
  9.     <application android:label="@string/app_name" android:icon="@drawable/property">
  10.  
  11.         <activity android:name=".demo1"
  12.  
  13.                  android:label="@string/app_name">
  14.  
  15.             <intent-filter>
  16.  
  17.                 <action android:name="android.intent.action.MAIN" />
  18.  
  19.                 <category android:name="android.intent.category.LAUNCHER" />
  20.  
  21.             </intent-filter>
  22.  
  23.         </activity>
  24.  
  25.     </application>
  26.  
  27. </manifest>
  28.  
  29.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
reynolds
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Mar 06, 2009 6:13 am
Location: Makati Philippines

Top

Postby mdownie » Thu Mar 12, 2009 5:00 pm

I think you have a couple of issues going on. First, the less important issue is that if I'm not mistaken to use the onClickListener, your buttons have to be final. Like: final Button b =... Secondly, and much more importantly, you are attempting to access a button that is not in that layout. You cannot access any part of a layout unless it is the current layout that is being shown. Not only that, but if you use findViewById() and you change the layout, you have to call the findViewById() again because the variable you set now is pointing to the wrong layout. This is true even if it is the same name on 2 different layouts. Just a tip though if you are switching back between 2 layouts, make sure to reinitialize all of the data for each layout every time the layout changes. Let me know if this resolves your issue. Thanks.
mdownie
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Nov 25, 2008 6:54 pm
Location: Las Vegas

Postby MrSnowflake » Thu Mar 12, 2009 8:16 pm

mdownie wrote:I think you have a couple of issues going on. First, the less important issue is that if I'm not mistaken to use the onClickListener, your buttons have to be final. Like: final Button b =...
That's not true, using regular (non final) variables works perfectly well.
mdownie wrote:Secondly, and much more importantly, you are attempting to access a button that is not in that layout. You cannot access any part of a layout unless it is the current layout that is being shown. Not only that, but if you use findViewById() and you change the layout, you have to call the findViewById() again because the variable you set now is pointing to the wrong layout. This is true even if it is the same name on 2 different layouts. Just a tip though if you are switching back between 2 layouts, make sure to reinitialize all of the data for each layout every time the layout changes. Let me know if this resolves your issue. Thanks.
What he says and calling setContentView from a listener, probably won't work either (not sure though), you should try it this way and if it doesn't work you have to call it using Activity.runOnUiThread().
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby reynolds » Fri Mar 13, 2009 3:12 am

I see...

I was able to fix the problem, and yes I now understand the fault from the original code. So this is the revised code I made, and now its running. But please bear with me since I'm totally noob to Java/Android.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package mrm.demo1;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8.  
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.widget.Button;
  13.  
  14.  
  15.  
  16. import android.view.View;
  17.  
  18. import android.view.Window;
  19.  
  20. import android.view.WindowManager;
  21.  
  22. import android.view.View.OnClickListener;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. public class demo1 extends Activity {
  29.  
  30.        
  31.  
  32.     /** Called when the activity is first created. */
  33.  
  34.     @Override
  35.  
  36.     public void onCreate(Bundle savedInstanceState) {
  37.  
  38.         super.onCreate(savedInstanceState);
  39.  
  40.         this.setFullscreen();
  41.  
  42.         this.setNoTitle();
  43.  
  44.  
  45.  
  46.         this.showHome();
  47.  
  48.        
  49.  
  50.  
  51.  
  52.     }
  53.  
  54.     ////////////////////////////////
  55.  
  56.     public void showInsurance()
  57.  
  58.     {
  59.  
  60.        
  61.  
  62.         setContentView(R.layout.insurance);
  63.  
  64.         Button btnShow = (Button)findViewById(R.id.btnShowHome);
  65.  
  66.         btnShow.setOnClickListener( new OnClickListener(){
  67.  
  68.                 @Override
  69.  
  70.                         public void onClick(View v)
  71.  
  72.                         {
  73.  
  74.                         onDestroy();
  75.  
  76.                         setContentView(R.layout.home);
  77.  
  78.                         showHome();
  79.  
  80.                        
  81.  
  82.                        
  83.  
  84.                         }
  85.  
  86.         });
  87.  
  88.        
  89.  
  90.     }
  91.  
  92.    
  93.  
  94.     ////////////////////////////////
  95.  
  96.     public void showHome()
  97.  
  98.     {
  99.  
  100.         setContentView(R.layout.home);
  101.  
  102.         Button btnShow = (Button)findViewById(R.id.btnShowInsurance);
  103.  
  104.         btnShow.setOnClickListener( new OnClickListener(){
  105.  
  106.                 @Override
  107.  
  108.                         public void onClick(View v)
  109.  
  110.                         {
  111.  
  112.                         onDestroy();
  113.  
  114.                         setContentView(R.layout.insurance);
  115.  
  116.                         showInsurance();
  117.  
  118.                        
  119.  
  120.                         }
  121.  
  122.         });
  123.  
  124.        
  125.  
  126.        
  127.  
  128.     }
  129.  
  130.     ////////////////////////////////
  131.  
  132.     public void setFullscreen() {
  133.  
  134.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  135.  
  136.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  137.  
  138.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  139.  
  140.     }
  141.  
  142.    ////////////////////////////////////
  143.  
  144.     public void setNoTitle() {
  145.  
  146.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  147.  
  148.     }    
  149.  
  150.    
  151.  
  152.    
  153.  
  154. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
reynolds
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Mar 06, 2009 6:13 am
Location: Makati Philippines

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests