应用程序启动时的“致命异常:主要”

时间:2012-02-28 22:28:56

标签: java android

在完成初学者教程之后,我最后结合了一堆课程来创建一个计时器应用程序,这需要花费一些时间并在时间过后播放媒体。该应用程序运行良好...昨天。我不记得改变什么,但是当我今天尝试调试时,应用程序会加载到我的手机,安装和崩溃。我收到大量错误消息,我不明白:

02-28 15:12:58.864: E/AndroidRuntime(26257): FATAL EXCEPTION: main
02-28 15:12:58.864: E/AndroidRuntime(26257): java.lang.RuntimeException: Unable to start activity ComponentInfo{digital.clock.activity/digital.clock.activity.DigitalActivity}: java.lang.NullPointerException
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.os.Looper.loop(Looper.java:130)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at java.lang.reflect.Method.invokeNative(Native Method)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at java.lang.reflect.Method.invoke(Method.java:507)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at dalvik.system.NativeStart.main(Native Method)
02-28 15:12:58.864: E/AndroidRuntime(26257): Caused by: java.lang.NullPointerException
02-28 15:12:58.864: E/AndroidRuntime(26257):    at digital.clock.activity.DigitalActivity.onCreate(DigitalActivity.java:74)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 15:12:58.864: E/AndroidRuntime(26257):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-28 15:12:58.864: E/AndroidRuntime(26257):    ... 11 more

我的主要活动看起来像......

package digital.clock.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.AlarmManager;
import android.app.PendingIntent;
import java.util.ArrayList;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;


public class DigitalActivity extends Activity 
{

private GestureLibrary gLib;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!gLib.load())
    {
        Toast.makeText(this, "Could not load Gesture Library", Toast.LENGTH_LONG).show();
        finish();
    }

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);



    final OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener() 
    {
        @Override
        public void onGesturePerformed(GestureOverlayView gestureView, Gesture gesture) 
        {

            ArrayList<Prediction> predictions = gLib.recognize(gesture);

            // one prediction needed
            if (predictions.size() > 0) 
            {
                Prediction prediction = predictions.get(0);
                // checking prediction
                if (prediction.score > 1.0) 
                {
                    // and action
                    timerAlert2();
                }
            }

        }
    };

    gestures.addOnGesturePerformedListener(handleGestureListener);





    //Create button and do something with intents...
    Button Activity2 = (Button) findViewById(R.id.Button01);
    Activity2.setOnClickListener(new View.OnClickListener()
    {
        public void onClick (View view)
        {
            Intent replyIntent = new Intent();
            setResult(RESULT_OK, replyIntent);
            finish();
        }
    });

    Button startTimer = (Button) findViewById(R.id.startTimer);
    startTimer.setOnClickListener(new View.OnClickListener()
    {
        public void onClick (View view)
        {
            timerAlert(view);
        }
    });

    Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
    stopAlarm.setOnClickListener(new View.OnClickListener()
    {
        public void onClick (View view)
        {
            stopService(new Intent(getBaseContext(), MediaPlayerService.class));
        }
    });
}




public void timerAlert (View view)
{

    //Declare the EditText object
    EditText textField = (EditText) findViewById(R.id.timeInSeconds);

    //Get user-entered number and convert to string

    int i = Integer.parseInt(textField.getText().toString());

    //Declare intent object
    Intent timerIntent = new Intent(this, TimerBroadcastReceiver.class);

    //Create PendingIntent and set parameters (context, code, intent object to use, constants)
    PendingIntent myPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, timerIntent, 0);

    //Create AlarmManager and assign it the ALARM_SERVICE
    AlarmManager myAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    //set AlarmManager parameters (type, TriggerTime, Operation)
    //RTC_WAKEUP will wake up the phone for the alarm, RTC will activate the alarm next time the phone wakes
    myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), myPendingIntent);

    Toast.makeText(this, "Alarm is set for " + i + " seconds!", Toast.LENGTH_LONG).show();
}

public void timerAlert2 ()
{
    int i = 5;

    //Declare intent object
    Intent timerIntent = new Intent(this, TimerBroadcastReceiver.class);

    //Create PendingIntent and set parameters (context, code, intent object to use, constants)
    PendingIntent myPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, timerIntent, 0);

    //Create AlarmManager and assign it the ALARM_SERVICE
    AlarmManager myAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    //set AlarmManager parameters (type, TriggerTime, Operation)
    //RTC_WAKEUP will wake up the phone for the alarm, RTC will activate the alarm next time the phone wakes
    myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 60000), myPendingIntent);

    Toast.makeText(this, "Alarm is set for " + i + " minutes!", Toast.LENGTH_LONG).show();
}

}

1 个答案:

答案 0 :(得分:0)

似乎没有找到你的按钮:(它在试图访问你在第73行实例化的按钮的行上崩溃了:Button01)。

搜索你的项目,它应该有一个名为res的子目录,其中应该有一个名为main.xml的文件。在其中你应该找到一个元素

 <Button
    android:id="@+id/button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

您应该验证您的项目是否包含此文件并在其中包含该行。如果您仍然遇到问题,请清理您的项目,以便更正。