2016-08-25 87 views
1

一旦我的應用程序啓動,我得到一個崩潰,我找不出原因。我有一個Service用作定時器來計算自定時器啓動以來的秒數,然後傳回到我的MainActivityAndroid java.lang.RuntimeException:接收廣播的錯誤Intent

MainActivity.java:

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = "BroadcastTest"; 
    private Intent intent; 

    private Chronometer timer; 
    private Button start; 
    private Button stop; 
    private EditText hourlyRate; 
    private TextView money; 
    private long timeWhenStopped = 0; 
    private double moneyEarned; 
    private double secondsRate; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     timer = (Chronometer) findViewById(R.id.chronometer); 
     start = (Button) findViewById(R.id.start); 
     stop = (Button) findViewById(R.id.stop); 
     hourlyRate = (EditText) findViewById(R.id.hourlyRate); 
     money = (TextView) findViewById(R.id.money); 

     intent = new Intent(this, Timer.class); 

      start.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       startService(new Intent(MainActivity.this, Timer.class)); 
       timer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped); 
       timer.start(); 

      } 
     }); 

     stop.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       timeWhenStopped = timer.getBase() - SystemClock.elapsedRealtime(); 
       timer.stop(); 
      } 
     }); 

     timer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() 
     { 
      @Override 
      public void onChronometerTick(Chronometer chronometer){ 
       long timeElapsed = SystemClock.elapsedRealtime() - timer.getBase(); 
       int seconds = (int) timeElapsed/1000; 

       double hrlyRate = Double.parseDouble(hourlyRate.getText().toString()); 
       secondsRate = (hrlyRate/60)/60; 

       moneyEarned = secondsRate * seconds; 

       double displayMoney = Math.round(moneyEarned * 100.0)/100.0; 

       Log.d("hours",Long.toString(seconds)); 

       money.setText("$"+Double.toString(displayMoney)); 

      } 
     }); 

     } 

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      updateUI(intent); 
     } 
    }; 

    @Override 
    public void onResume() { 
     super.onResume(); 
     startService(intent); 
     registerReceiver(broadcastReceiver, new IntentFilter(Timer.BROADCAST_ACTION)); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     unregisterReceiver(broadcastReceiver); 
     stopService(intent); 
    } 

    private void updateUI(Intent intent) { 
     String counter = intent.getStringExtra("counter"); 
     Log.d("SERVICE_SECONDS", counter); 
    } 
} 

Timer.java:

public class Timer extends Service { 

    private static final String TAG = "BroadcastService"; 
    public static final String BROADCAST_ACTION = "b.calvin.com.dirtymoney"; 
    private final Handler handler = new Handler(); 
    Intent intent; 
    int counter = 0; 

    private Runnable sendUpdatesToUI = new Runnable() { 
     public void run() { 
      countSeconds(); 
      handler.postDelayed(this, 1000); // 1 second 
     } 
    }; 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     intent = new Intent(BROADCAST_ACTION); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     handler.removeCallbacks(sendUpdatesToUI); 
     handler.postDelayed(sendUpdatesToUI, 0); // 1 second 

     Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 
      return START_STICKY; 
     } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    private void countSeconds() { 
     Log.d(TAG, Integer.toString(++counter)); 
     sendBroadcast(intent); 
    } 
} 

錯誤:

FATAL EXCEPTION: main 
Process: b.calvin.com.dirtymoney, PID: 3348 
java.lang.RuntimeException: Error receiving broadcast Intent { act=b.calvin.com.dirtymoney flg=0x10 } in [email protected] 
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:891) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: println needs a message 
at android.util.Log.println_native(Native Method) 
at android.util.Log.d(Log.java:139) 
at b.calvin.com.dirtymoney.MainActivity.updateUI(MainActivity.java:113) 
at b.calvin.com.dirtymoney.MainActivity.access$600(MainActivity.java:20) 
at b.calvin.com.dirtymoney.MainActivity$4.onReceive(MainActivity.java:93) 
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:881) 
at android.os.Handler.handleCallback(Handler.java:739)  
at android.os.Handler.dispatchMessage(Handler.java:95)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5417)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

您通過廣播接收器傳遞錯誤的意圖。它在MainActivity的updateUI()上產生空指針異常 –

+0

解釋?我不明白sry – BigFatToe

+0

你沒有通過廣播接收機的意圖傳遞任何額外的數據。但是Mainactivity中的你的updateUI(Intent intent)檢查了一個額外的字符串,但沒有發現任何東西並且引發了空指針異常 –

回答

2

好像你FO rgot將你的counter變量放入Intent,所以你通過nullLog.d()作爲消息,導致NullPointerException

像這樣的東西應該工作:

private void countSeconds() { 
    Log.d(TAG, Integer.toString(++counter)); 

    Intent counterIntent = new Intent(BROADCAST_ACTION); 
    counterIntent.putExtra("counter", counter); 

    sendBroadcast(counterIntent); 
} 

此外,計數器是int,所以你應該叫getIntExtra()而不是getStringExtra()

private void updateUI(Intent intent) { 
    int counter = intent.getIntExtra("counter"); 
    Log.d("SERVICE_SECONDS", Integer.toString(counter)); 
} 
+0

非常感謝! – BigFatToe