setOnclickListener made the Activity crashed

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

setOnclickListener made the Activity crashed

Postby vycon » Sun May 23, 2010 4:09 am

Here is my code, I am trying trigger an event that opens another screen by click button.

Can you help me with this problem? Thanks.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Diary extends Activity {
  2.        
  3.         @Override
  4.         protected void onCreate(Bundle savedInstanceState) {
  5.        
  6.                 super.onCreate(savedInstanceState);
  7.                 setContentView(R.layout.diary_view);
  8.                
  9.                 // Initialize widget
  10.                 final Button button_timeDatePicker = (Button)findViewById(R.id.getDateTimeButton);
  11.                
  12.                 // Initialize data fields for diary
  13.                 button_timeDatePicker.setOnClickListener(new View.OnClickListener() {
  14.                        
  15.                         @Override
  16.                         public void onClick(View v) {
  17.                                 Intent intent_getDateTime = new Intent(Diary.this, DateTimePicker.class);
  18.                                 startActivity(intent_getDateTime);
  19.                         }
  20.                 });
  21.         }
  22. }
  23.  
  24. public class DateTimePicker extends Activity {
  25.         @Override
  26.         protected void onCreate(Bundle savedInstanceState) {
  27.                 super.onCreate(savedInstanceState);
  28.                 setContentView(R.layout.pickers_view);
  29.                
  30.                 Button button_getDateTime = (Button) findViewById(R.id.getDateTimeButton);
  31.                
  32.                 button_getDateTime.setOnClickListener(new View.OnClickListener() {
  33.                         @Override
  34.                         public void onClick(View v) {
  35.                                 setResult(RESULT_OK);
  36.                                 finish();
  37.                         }
  38.                 });
  39.         }
  40. }
  41.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
vycon
Freshman
Freshman
 
Posts: 7
Joined: Sun May 23, 2010 4:03 am

Top

Re: setOnclickListener made the Activity crashed

Postby anarche » Sun May 23, 2010 4:36 am

Have you bound the .xml layout file to the Activity.

Without your error log I can't know for sure, but I'd guess you're getting NullPointerException because the Views are not bound, and the call to findViewById is returning null
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: setOnclickListener made the Activity crashed

Postby gentleboy » Fri May 28, 2010 1:22 pm

Hello,java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package myfirst.foo;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8.  
  9. public class Hello extends Activity {
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.        
  15.        
  16.         findViewById(R.id.Button01).setOnClickListener(
  17.                 new OnClickListener() {
  18.             public void onClick(View v) {
  19.                 Intent i = new Intent(Hello.this, newactivity.class);
  20.                 startActivity(i);
  21.             }
  22.         });
  23.        
  24.     }
  25. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

newactivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package myfirst.foo;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class newactivity extends Activity {
  7.  
  8.     @Override
  9.     public void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.new_main);  
  12.     }
  13. }[quote][/quote]
  14.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:layout_width="fill_parent"
  4.    android:layout_height="fill_parent"
  5.    android:gravity="center"
  6.    android:orientation="vertical"
  7.    >
  8.    
  9. <Button android:text="@+id/Button01"
  10. android:id="@+id/Button01"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content">
  13. </Button>
  14. </LinearLayout>
  15.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



new_mail.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  android:layout_width="fill_parent"
  4.  android:layout_height="fill_parent">
  5.  
  6.     <TextView
  7.         android:id="@+id/Textview05"
  8.         android:textSize="15sp"
  9.         android:background="#FFFFFF"
  10.         android:textColor="#000000"
  11.    android:layout_width="fill_parent"
  12.    android:layout_height="fill_parent"
  13.    android:text="News Description goes here"
  14.    />
  15.      
  16.  
  17. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



why the application stops unexpectedly when the button in hello.java is clicked and it starts a new activity newactivity.java?
please help!!!
gentleboy
Once Poster
Once Poster
 
Posts: 1
Joined: Thu May 27, 2010 3:17 pm

Re: setOnclickListener made the Activity crashed

Postby anarche » Fri May 28, 2010 1:57 pm

Because you are telling it to.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: setOnclickListener made the Activity crashed

Postby phix » Sun May 30, 2010 7:34 am

This is probably the issue... doing it this way is a little too "short hand" I think.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. findViewById(R.id.Button01).setOnClickListener(new OnClickListener() {
  2.     public void onClick(View v) {
  3.         Intent i = new Intent(Hello.this, newactivity.class);
  4.         startActivity(i);
  5.     }
  6. });
  7.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Try changing it to

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Button startNewBtn = (Button)findViewById(R.id.Button01);
  2. startNewBtn.setOnClickListener(new OnClickListener() {
  3.     public void onClick(View v) {
  4.         Intent i = new Intent(Hello.this, newactivity.class);
  5.         startActivity(i);
  6.     }
  7. });
  8.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Re: setOnclickListener made the Activity crashed

Postby vycon » Sat Jun 05, 2010 6:38 am

Thanks for your help, guys.
vycon
Freshman
Freshman
 
Posts: 7
Joined: Sun May 23, 2010 4:03 am

Top

Re: setOnclickListener made the Activity crashed

Postby thunder » Sat Jun 05, 2010 11:23 am

I Have the Same Problem.....
Its shows the Error ........


Please Help me to solve this problem.
thunder
Freshman
Freshman
 
Posts: 8
Joined: Sat Jun 05, 2010 11:14 am

Re: setOnclickListener made the Activity crashed

Postby anarche » Sun Jun 06, 2010 3:40 am

Whats the error?

Please post logs.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: setOnclickListener made the Activity crashed

Postby phix » Mon Jun 07, 2010 7:07 pm

thunder wrote:I Have the Same Problem.....
Its shows the Error ........


Please Help me to solve this problem.


There's about 3 million exceptions, all of which are because the developer isn't doing something right, forgot to declare something, or otherwise. The log outputs a package and a line number for you.
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests