2016-08-16 103 views
-1

是的,我讀過thisthis,但沒有成功。這是我想要做的:絕對不知道如何解決這個空指針異常

我已經創建了這個應用程序,在用戶輸入秒數並按下按鈕之後,服務啓動,關閉屏幕,啓動一個計時器(基於在輸入的秒數上)並且僅在所述計時器結束時停止。停止後,屏幕打開並且TextView被更改。現在,想法是,如果用戶在計時器結束之前打開屏幕,則服務停止並且TextView不會更改。

我的問題來的時候,手動後打開屏幕上,如果輸入的應用程序,並殺死它,我收到以下錯誤:

java.lang.RuntimeException: Unable to start service [email protected] with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference 

所說的誤差點,這行代碼(從MainService) :

usedTimer = intent.getStringExtra("timer"); 

閱讀上述鏈接後,我試着圍繞「usedTimer」執行try-catch,但它沒有改變任何東西。我也嘗試用「」(空白)初始化「usedTimer」,但是,再次沒有成功。

MainService:

public class MainService extends Service { 

static String BROADCAST_ACTION = "com.example.vladpintea.friendsbeforecents.displayevent"; 
Handler handler = new Handler(); 
int counterr = 0; 
Intent intentt; 

String usedTimer; 
long interval; 

//TimerTask that will cause the run() runnable to happen. 
TimerTask myTask = new TimerTask() { 
    public void run() { stopSelf(); } 
}; 
//Timer that will make the runnable run. 
Timer myTimer = new Timer(); 

@Override 
public void onCreate() { 
    Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show(); 

    intentt = new Intent(BROADCAST_ACTION); 

    registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON)); 
} 

private BroadcastReceiver counter = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     myTimer.cancel(); 

     Toast.makeText(MainService.this, "Whoops! You've Lost.", Toast.LENGTH_SHORT).show(); 

     Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000); 
    } 
}; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Toast.makeText(MainService.this, "Service, Started", Toast.LENGTH_SHORT).show(); 

    usedTimer = intent.getStringExtra("timer"); 
    try { 
     interval = Long.parseLong(usedTimer); 
    } catch (NumberFormatException ignored) {} 

    myTimer.schedule(myTask, interval); 

    handler.removeCallbacks(sendUpdatesToUI); 
    handler.postDelayed(sendUpdatesToUI, 1000); 

    return super.onStartCommand(intent, flags, startId); 
} 

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

public void DisplayLoggingInfoPlus() { 
    intentt.putExtra("counter", String.valueOf(++counterr)); 
    sendBroadcast(intentt); 
} 

@Override 
public void onDestroy() { 
    unregisterReceiver(counter); 

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK 
      | PowerManager.ACQUIRE_CAUSES_WAKEUP 
      | PowerManager.ON_AFTER_RELEASE, "MyWakeLock"); 
    if ((wakeLock != null) && (!wakeLock.isHeld())) { wakeLock.acquire(); } 

    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000); 

    DisplayLoggingInfoPlus(); 
} 

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

MainActivity:

public class MainActivity extends AppCompatActivity { 

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

    final Button startApp = (Button) findViewById(R.id.startApp); 
    final EditText timer = (EditText) findViewById(R.id.insertTimer); 

    assert startApp != null; 
    startApp.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show(); 

      Intent startMain = new Intent(Intent.ACTION_MAIN); 
      startMain.addCategory(Intent.CATEGORY_HOME); 
      startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(startMain); 

      Intent intent = new Intent(MainActivity.this, MainService.class); 
      assert timer != null; 
      intent.putExtra("timer", timer.getText().toString()); 
      startService(intent); 

      Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000); 

      registerReceiver(broadcastReceiver, new IntentFilter(MainService.BROADCAST_ACTION)); 
     } 
    }); 
} 

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

private void updateUI(Intent intent) { 
    TextView txtCounter = (TextView) findViewById(R.id.txtCounter); 
    String counter = intent.getStringExtra("counter"); 

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString(counter,String.valueOf(txtCounter)); 
    editor.apply(); 

    assert txtCounter != null; 
    txtCounter.setText(counter); 
} 

@Override 
public void onDestroy() { 
    try { unregisterReceiver(broadcastReceiver); } catch (IllegalArgumentException ignored) {} 

    super.onDestroy(); 
} 
+0

你知道你有'intent'和'intentt'嗎? –

+0

@PaoloForgia是的,但這些都是故意的。不要認爲他們是問題。 –

+0

@VladPintea是的,只是檢查,因爲在第一次看起來這似乎很奇怪 –

回答

2

它越來越空值,而您試圖使它拋出空指針異常解析空值。 你可以使用這段代碼來處理這個異常;

if(intent==null) 
     return -1; 
    if(intent.getStringExtra("timer")==null) 
     return -1; 

     usedTimer = intent.getStringExtra("timer");