广播接收器强制关闭我的申请

时间:2012-03-03 15:59:01

标签: android broadcastreceiver

我创建了一个广播接收器来启动onReceive活动,如下所示:

public class BroadcastReceiver1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    Toast.makeText(context, "Broadcast radi", Toast.LENGTH_SHORT).show();

    Intent intent1 = new Intent(context, ShootTheDuckActivity.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent1); 

}

}

但是我的申请人关闭了,它在LogCat中给出了这个:

03-03 16:58:31.454: D/dalvikvm(3738): GC_EXTERNAL_ALLOC freed 120K, 51% free 2700K/5447K, external 1034K/1038K, paused 54ms
03-03 16:58:59.834: D/dalvikvm(3738): GC_EXTERNAL_ALLOC freed 117K, 50% free 2769K/5511K, external 1374K/1558K, paused 57ms
03-03 16:58:59.973: D/AndroidRuntime(3738): Shutting down VM
03-03 16:58:59.973: W/dalvikvm(3738): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-03 16:58:59.995: E/AndroidRuntime(3738): FATAL EXCEPTION: main
03-03 16:58:59.995: E/AndroidRuntime(3738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.etfpodgorica.WakeUpAlarm/com.etfpodgorica.WakeUpAlarm.ShootTheDuckActivity}: java.lang.NullPointerException
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.os.Looper.loop(Looper.java:123)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at java.lang.reflect.Method.invoke(Method.java:507)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at dalvik.system.NativeStart.main(Native Method)
03-03 16:58:59.995: E/AndroidRuntime(3738): Caused by: java.lang.NullPointerException
03-03 16:58:59.995: E/AndroidRuntime(3738):     at com.etfpodgorica.WakeUpAlarm.ShootTheDuckActivity.onCreate(ShootTheDuckActivity.java:61)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 16:58:59.995: E/AndroidRuntime(3738):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-03 16:58:59.995: E/AndroidRuntime(3738):     ... 11 more

ShootTheDuckActivity如下所示:

public class ShootTheDuckActivity extends Activity {

public static String filename="Settings";
SharedPreferences data;
SharedPreferences.Editor editor;
int tezinaProblema=0;//0-Easy; 1-Medium; 2-Hard
static double brzina = 1;

int indikator=0;
static MediaPlayer shot;
MediaPlayer explosion;
MediaPlayer player;
private boolean PozivIzMenija=false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shoottheduck);

    Toast.makeText(ShootTheDuckActivity.this, "Shoot the bird to shut down alarm",   Toast.LENGTH_LONG).show();

    data=getSharedPreferences(filename,0);
    editor=data.edit();
    tezinaProblema=data.getInt("Lvl:", 0);
    if (tezinaProblema == 0) {
        brzina = 1;
    } else {
        if (tezinaProblema == 1) {
            brzina = 0.5;
        } else {
            if (tezinaProblema == 2) {
                brzina = 0.2;
            }
        }
    }

    Bundle podesavanja = getIntent().getExtras();
    PozivIzMenija=podesavanja.getBoolean("PozivIzMenija");
    if(PozivIzMenija==false){ 

        player = MediaPlayer.create(this, R.raw.demo);  //vec postoji
        player.setLooping(true); // Set looping      //vec postoji
        player.start();              //vec postoji

    }

    shot = MediaPlayer.create(this, R.raw.pucanj);
    shot.setLooping(false);

    explosion = MediaPlayer.create(this, R.raw.explosion);
    explosion.setLooping(false);

    final ImageView button = (ImageView) findViewById(R.id.imageViewSD1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                indikator=1;

                if(PozivIzMenija==false){ 
                    player.stop();   //vec postoji
                }

                button.setImageResource(R.drawable.background);
                button.setBackgroundResource(R.drawable.explosion);
                AnimationDrawable explosionAnimation = (AnimationDrawable) button.getBackground();

                explosionAnimation.start();
                explosion.start();

                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    public void run() {
                        finish();
                    }
                }, 1100);

                //finish();
            }
    });
    Thread thr = new Thread(mTask);
    thr.start();

}

Runnable mTask = new Runnable() {
    public void run() {
        // just sleep for x seconds.
        int i;
        i=1;
        while (i!=0) {
                    try {
                        Thread.sleep((long) (50*brzina));
                        runOnUiThread(done);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    i=i+1;
                    if (indikator==1) {break;}
        }
        }
    };
    int a=10;
    int b=10;
    int a1=10;
    int b1=10;
    static int left;
    static int right;
    static int top;
    static int bottom;
    Runnable done = new Runnable() {
        public void run() {
            final ImageView button = (ImageView) findViewById(R.id.imageViewSD1);
            if (button.getRight()>getApplicationContext().getResources().getDisplayMetrics().widthPixels*0.95) {
                a=(-1)*a1;
            } 
            else {
                if (button.getLeft()<10) {
                    a=a1;
            }
            }
            if (button.getBottom()>getApplicationContext().getResources().getDisplayMetrics().heightPixels*0.7) {
                b=(-1)*b1;
            }
            else {
                if (button.getTop()<10) {
                    b=b1;
                }
            }

            button.post(new Runnable() {
                public void run() {
                    //button.offsetLeftAndRight(a);
                    //button.offsetTopAndBottom(b);
                    //button.bringToFront();
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp = (LinearLayout.LayoutParams) button.getLayoutParams();
                    left = lp.leftMargin;
                    right = lp.rightMargin;
                    top = lp.topMargin;
                    bottom = lp.bottomMargin;
                    lp.setMargins(left+a, top+b, right-a, bottom-b);
                    button.setLayoutParams(lp);
                }
            }); 
            //button.offsetLeftAndRight(a);
            //button.offsetTopAndBottom(b);
            //button.setText("aaa");

            }
        };

        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                shot.start();
                return true;
            }
            return super.onTouchEvent(event);
        }

        public void onDestroy() {
            super.onDestroy(); 
            if(PozivIzMenija==false){ 
                player.stop();   //vec postoji
            }
        }

        public void onBackPressed() {
            if(PozivIzMenija==true){
                finish();
                return ;
            }

            PendingIntent pendingIntent;
            Context mContext = this.getApplicationContext();
            Intent intent = new Intent();
            Calendar c = Calendar.getInstance();
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Random generator = new Random();
            int i = generator.nextInt(1000000);

            intent.setClass(mContext, ShootTheDuckActivity.class);
            pendingIntent =  PendingIntent.getActivity(mContext, i, intent, 0);
            c.set(Calendar.MINUTE,c.get(Calendar.MINUTE)+1);
            am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);

            Toast.makeText(ShootTheDuckActivity.this, "Snooze", Toast.LENGTH_LONG).show();

            finish();
        }
}

1 个答案:

答案 0 :(得分:0)

嗯,首先,你没有提供一个 Bundle ,你正在使用 Intent 来开始你的射击活动。从您粘贴的代码中,很难确定 onCreate 中第61行的确切内容(您可能希望在您的问题中包含该花絮)。但是,作为第一个猜测 - 在活动中,您明确假设 Intent#getExtras()不会返回null(它可以)。也许在这里添加一个空检查?

Bundle podesavanja = getIntent().getExtras();
PozivIzMenija=podesavanja.getBoolean("PozivIzMenija");