Android NullPointerException(println需要一条消息)

弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述Android NullPointerException:println需要一条消息相关的知识,希望能为你提供帮助。
我正在制作一个假的通话应用程序,我已经选择了快速拨打电话。我在quickCall()方法中拥有此选项的所有逻辑。
这是MainActivity的代码

public class MainActivity extends AppCompatActivity {public static final String LOG_TAG = "MainActivity"; ImageView imageView; EditText number, name; Button setTimeButton; ImageButton imageButton; TimePickerDialog timePicker; long selectedTimeInMillis; String enteredName, enteredNumber; private static final int SELECT_IMAGE = 100; public static final int REQUEST_CODE_PI = 1001; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PreferenceManager.setDefaultValues(this, R.xml.settings, false); imageView = findViewById(R.id.image_view); setTimeButton = findViewById(R.id.set_time); imageButton = findViewById(R.id.place_call_button); number = findViewById(R.id.number); name = findViewById(R.id.name); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent galleryIntent = new Intent(Intent.ACTION_PICK); galleryIntent.setType("image/*"); startActivityForResult(galleryIntent, SELECT_IMAGE); } }); setTimeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); timePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); selectedTimeInMillis = calendar.getTimeInMillis(); if (hourOfDay > 12) { hourOfDay = hourOfDay - 12; }Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show(); Log.v(LOG_TAG, "Selected time in millis:" + selectedTimeInMillis); } }, hour, minute, false); timePicker.show(); } }); imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { enteredName = name.getText().toString().trim(); enteredNumber = number.getText().toString().trim(); if (enteredNumber.isEmpty()){ Toast.makeText(MainActivity.this, "Please entera number", Toast.LENGTH_LONG).show(); return; } Intent intent = new Intent(MainActivity.this, AlarmReciever.class); intent.putExtra("FAKE_NAME", enteredName); intent.putExtra("FAKE_NUMBER", enteredNumber); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE_PI, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); if (alarmManager != null) { alarmManager.set(AlarmManager.RTC_WAKEUP, selectedTimeInMillis, pendingIntent); Toast.makeText(MainActivity.this, "Your call has been placed", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Error: something wrong", Toast.LENGTH_SHORT).show(); } } }); }@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; }@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.settings: Intent intent = new Intent(MainActivity.this, SettingsActivity.class); startActivity(intent); return true; case R.id.quick_call: quickCall(); return true; default: return super.onOptionsItemSelected(item); } }@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { try { Uri imageUri = data.getData(); InputStream imageStream = null; if (imageUri != null) { imageStream = getContentResolver().openInputStream(imageUri); } Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); imageView.setImageBitmap(selectedImage); } catch (FileNotFoundException e) { e.printStackTrace(); Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); }} else { Toast.makeText(this, "You haven't picked any Image", Toast.LENGTH_SHORT).show(); } }public void quickCall(){Calendar calendar = Calendar.getInstance(); calendar.get(Calendar.HOUR_OF_DAY); calendar.get(Calendar.MINUTE); long millis = calendar.getTimeInMillis(); Intent quickCallIntent = new Intent(MainActivity.this, AlarmReciever.class); PendingIntent quickCallPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE_PI, quickCallIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); if (alarmManager != null) { alarmManager.set(AlarmManager.RTC_WAKEUP, millis + 10000, quickCallPendingIntent); Toast.makeText(MainActivity.this, "Your call has been placed", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Error: something wrong", Toast.LENGTH_SHORT).show(); } } }

这是AlarmReceiver
public class AlarmReciever extends BroadcastReceiver {@Override public void onReceive(Context context, Intent intent) { String getFakeName = intent.getStringExtra("FAKE_NAME"); String getFakeNumber = intent.getStringExtra("FAKE_NUMBER"); Intent fakeRinger = new Intent(); fakeRinger.setClassName("com.example.mani.fakecall", "com.example.mani.fakecall.FakeRinger"); fakeRinger.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); fakeRinger.putExtra("USER_FAKE_NAME", getFakeName); fakeRinger.putExtra("USER_FAKE_NUMBER", getFakeNumber); context.startActivity(fakeRinger); Log.v("Fake name is", getFakeName); Log.v("Fake number is", getFakeNumber); } }

这是FakeRinger活动
public class FakeRinger extends AppCompatActivity {TextView displayName, displayNumber, displayCarrier; ImageButton alarmOffButton; String carrierName; ImageView displayImage; public static final int REQUEST_CODE = 1001; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fake_ringer); displayName = findViewById(R.id.display_name); displayNumber = findViewById(R.id.display_number); displayCarrier = findViewById(R.id.network_operator); displayImage = findViewById(R.id.display_image); TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if (telephonyManager != null) { carrierName = telephonyManager.getNetworkOperatorName(); }if (carrierName != null) { displayCarrier.setText(getResources().getString(R.string.incoming_call, carrierName)); } else { displayCarrier.setText(getResources().getString(R.string.incoming_call)); }Intent intent = getIntent(); String userFakeName = intent.getExtras().getString("USER_FAKE_NAME"); displayName.setText(userFakeName); String userFakeNumber = intent.getExtras().getString("USER_FAKE_NUMBER"); displayNumber.setText(userFakeNumber); alarmOffButton = findViewById(R.id.hang_up_call); alarmOffButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), AlarmReciever.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); stopService(intent); if (alarmManager != null) { alarmManager.cancel(pendingIntent); } } }); } }

每当我点击快速通话选项,我的应用程序崩溃。这是我得到的错误 -java.lang.RuntimeException: Unable to start receiver com.example.mani.fakecall.AlarmReciever: java.lang.NullPointerException: println needs a message
答案当您尝试在qazxsw poi中打印值,并且值为null时,您会收到错误消息:
java.lang.NullPointerException:println需要一条消息
所以你可以验证一个空值:
LogCat

要不就
Log.v("Fake name is", getFakeName!=null?getFakeName:"Not determinated"); Log.v("Fake number is", getFakeNumber!=null?getFakeNumber:"Not determinated");

如果我们查看if(getFakeName != null){ Log.v("Fake name is", getFakeName); } if(getFakeNumber != null){ Log.v("Fake number is", getFakeNumber); } 的源代码,则消息需要String值(不是空值):
Log.v()

另一答案只需在将值打印到logcat之前添加检查:
public static int v(String tag, String msg, Throwable tr) { return println(LOG_ID_MAIN, VERBOSE, tag, msg + ' ' + getStackTraceString(tr)); }

你需要将值传递给if(getFakeName != null) Log.v("Fake name is", getFakeName); if(getFakeNumber != null) Log.v("Fake number is", getFakeNumber); ,这不能是Log.v()
另一答案检查你是否在qazxsw poi中声明了AlarmReceiver
【Android NullPointerException(println需要一条消息)】null

    推荐阅读