应用继续崩溃

时间:2015-10-19 15:36:12

标签: java android android-studio

抱歉,我是新手,但我正在开发的应用程序在我转到某个菜单后继续崩溃,你能告诉我如何修复它吗?这是我的logcat以及下面的编码和xml文件:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.TTTTT.ifelse.alarm.onCreate(alarm.java:27)

代码:

        btnAlarm1 = (Button) findViewById(R.id.btnAlarm);
        btnstop = (Button) findViewById(R.id.btn1);
        txtSeconds1 = (EditText) findViewById(R.id.txtSeconds);

        btnAlarm1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    int i = Integer.parseInt(txtSeconds1.getText().toString());
                    Intent intent = new Intent(alarm.this, AlarmReceiver.class);
                    PendingIntent pendingIntent =
                            PendingIntent.getActivity(alarm.this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);

                    if (mToast != null) {
                        mToast.cancel();
                    }
                    mToast = Toast.makeText(getApplicationContext(), "Alarm set in:" + i + "seconds", Toast.LENGTH_LONG);
                    mToast.show();

                }catch (NumberFormatException e)
                {
                    if (mToast != null)
                    {mToast.cancel();}
                    mToast = Toast.makeText(alarm.this, "Enter Number", Toast.LENGTH_LONG);
                    mToast.show();
                    Log.i("MainActivity", "Number format exception");
                }
            }

        });
        btnstop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(alarm.this, AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(alarm.this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.cancel(pendingIntent);

                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(getApplicationContext(), "Alarm Cancelled!", Toast.LENGTH_LONG);
                mToast.show();
            }
        });

    }


}

*Xml file here 
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.TTTTT.ifelse.Alarm">

   <EditText
    android:id="@+id/txtSeconds"
    android:inputType="number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

   <Button
    android:text="Set Alarm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnAlarm"
    android:layout_below="@+id/txtSeconds"
    android:layout_centerHorizontal="true" />

    <Button
    android:text="Stop Alarm"
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnAlarm"
    android:layout_centerHorizontal="true" />

     </RelativeLayout>*

0 个答案:

没有答案