java android crash setOnClickListener

时间:2016-06-02 18:57:13

标签: java android

我是Android新手,我试图让我的应用程序适用于所有手机 它适用于23 API,并且在每次打开应用程序时都无法在19 API kitkat中运行

有没有办法解决这个问题? 你能告诉我我的问题是什么并向我解释,

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();
                    }
                }
            }
    );
}

}

- 错误消息

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) 

主要活动:

<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"/>

这是我的完整问题,我希望有人可以帮助我

2 个答案:

答案 0 :(得分:3)

您正在调用视图的方法而不进行初始化。

您需要先初始化视图,然后调用方法

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

答案 1 :(得分:0)

检查一下,

正如@Rod_所提及的,并且从错误日志中,它清楚地表明视图未初始化。

  

显示java.lang.NullPointerException                                                                                    在   com.aabdelrahman730yahoo.mydesign.MainActivity.addListerOnButton(MainActivity.java:51)                                                                                    在   com.aabdelrahman730yahoo.mydesign.MainActivity.onCreate(MainActivity.java:43)

确保在您的java代码中初始化了以下按钮。

ButtonStart和ButtonReset - 看起来这里没有初始化Buttonreset。

更改您的代码,如下所示。 在oncreate下

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

希望看起来很清楚.. !!

<强>更新

您可能未初始化媒体播放器。 问题可能还在于媒体播放器的使用。请检查下面的代码。

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

在你的情况下

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

添加上述行以调用函数。