BeBo1
BeBo1

Reputation: 11

java android crash setOnClickListener

i am new in android and i was trying to make my app works in all phones it works in 23 API and don't work in 19 API kitkat it crashes each time i open the application

Is there a way to fix this problem ? and could you tell me what was my problem and explain it to me,

public class MainActivity extends AppCompatActivity {
private Button ButtonStart,ButtonReset ;
private TextView Number ;
private CountDownTimer myTimer ;
private MediaPlayer TimePassesSound;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButtonStart = (Button)findViewById (R.id.button); //initialize view
    Number = (TextView)findViewById (R.id.textView); //initialize view
    ButtonReset = (Button)findViewById (R.id.button2);
    MediaPlayer TimePassesSound;
    TimePassesSound = new MediaPlayer();
    TimePassesSound = MediaPlayer.create(getApplicationContext(), R.raw.time_passing);
    addListerOnButton (); //call method of the view
    addListerOnButton2 (); //call method of the view
}

public void addListerOnButton () {
    ButtonStart.setOnClickListener (
            new View.OnClickListener () {
                public void onClick(View v) {
                    if (ButtonStart.getText ().toString () != "Stop") {
                        int StartTime = Integer.parseInt (Number.getText ().toString ());
                        myTimer = new CountDownTimer (StartTime*1000, 1000) {
                            public void onTick(long millisUntilFinished) {
                                ButtonStart.setText ("Stop");
                                Number.setText (""+millisUntilFinished / 1000);
                            }
                            public void onFinish() {
                                Number.setText("60");
                                ButtonStart.setText ("Start");
                                TimePassesSound.setLooping(false);
                                TimePassesSound.start();
                            }
                        }.start();
                    } else {
                        ButtonStart.setText ("Start");
                        myTimer.cancel ();
                    }
                }
            }
    );
}


public void addListerOnButton2 () {
    ButtonReset.setOnClickListener (
            new View.OnClickListener () {
                public void onClick(View v) {
                    if ("Start".equals (ButtonStart.getText ().toString())) {
                        Number.setText ("60");
                        myTimer.cancel ();
                    }else{
                        Toast.makeText (MainActivity.this,"You must stop the countdown first",Toast.LENGTH_LONG).show();
                    }
                }
            }
    );
}

}

public void addListerOnButton2 () {
    ButtonReset.setOnClickListener (
            new View.OnClickListener () {
                public void onClick(View v) {
                    if ("Start".equals (ButtonStart.getText ().toString())) {
                        Number.setText ("60");
                        myTimer.cancel ();
                    }else{
                        Toast.makeText (MainActivity.this,"You must stop the countdown first",Toast.LENGTH_LONG).show();
                    }
                }
            }
    );
}

}

-- Error messages

Process: com.aabdelrahman730yahoo.mydesign, PID: 3627
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aabdelrahman730yahoo.mydesign/com.aabdelrahman730yahoo.mydesign.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.aabdelrahman730yahoo.mydesign.MainActivity.addListerOnButton(MainActivity.java:51)
    at com.aabdelrahman730yahoo.mydesign.MainActivity.onCreate(MainActivity.java:43)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
    at android.app.ActivityThread.access$800(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method) 

Main Activity :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.aabdelrahman730yahoo.mydesign.MainActivity"
android:touchscreenBlocksFocus="false"
android:background="#34416a">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reset"
    android:id="@+id/button2"
    android:layout_above="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="60"
    android:id="@+id/textView"
    android:singleLine="true"
    android:textSize="80dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"/>

That's was my full problem i hope someone could help me with

Upvotes: 0

Views: 114

Answers (2)

Swaminathan V
Swaminathan V

Reputation: 4781

Check this,

As @Rod_ mentioned and from error log, it clearly states that view is not initialized.

java.lang.NullPointerException at com.aabdelrahman730yahoo.mydesign.MainActivity.addListerOnButton(MainActivity.java:51) at com.aabdelrahman730yahoo.mydesign.MainActivity.onCreate(MainActivity.java:43)

Make sure following buttons were initialized in your java code.

ButtonStart and ButtonReset -- Buttonreset is not initialized here it seems.

Change your code like below. under oncreate

ButtonStart = (Button)findViewById (R.id.button);
ButtonReset = (Button)findViewById (R.id.button_); --> You missed it ..
Number = (TextView)findViewById (R.id.textView);
addListerOnButton ();
addListerOnButton2 ();

Hope it seems clear..!!

UPDATE:

You may not initialized the media player. The problem may be also due to usage of media player. Kindly check the code below.

MediaPlayer mediaPlayer;
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.time_passing);

in your case

MediaPlayer TimePassesSound;
TimePassesSound = new MediaPlayer();
TimePassesSound = MediaPlayer.create(getApplicationContext(), R.raw.time_passing);

Add the above lines befor calling the functions.

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

You are calling the method of the view without initializing it.

You need to initialize the view first and then call the method

ButtonStart = (Button)findViewById (R.id.button); //initialize view Number = (TextView)findViewById (R.id.textView); //initialize view addListerOnButton (); //call method of the view addListerOnButton2 (); //call method of the view

Upvotes: 3

Related Questions