Android Studio - 无法在通知中显示字符串

Android Studio - Can't display string in notification

我又遇到了一个问题。我试图在状态栏通知中显示电池信息,但当我打开应用程序的设置时,我的应用程序崩溃了。我在这里不知所措,我不知道此时该怎么办。 logcat 表示:

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual       
    method 'java.lang.CharSequence android.widget.TextView.getText()' on a    
    null object reference

这是我的代码:

 public class SettingsActivity extends ActionBarActivity implements     
 OnClickListener
 {
  Button start, clear;

Notification noti;
NotificationManager nmgr;
public static final int NOTIFICATION_ID = 0;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this);
    setContentView(R.layout.activity_settings);
    TextView batteryInfo = (TextView)findViewById(R.id.batteryInfo);
    String battinfo = batteryInfo.getText().toString();


    getInit();

    nmgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    noti = new Notification(R.drawable.flame, "Battery Temperature", System.currentTimeMillis());
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    //float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)) / 10;

    noti.setLatestEventInfo(this, "Battery Info", battinfo, pIntent);
    noti.flags = Notification.FLAG_ONGOING_EVENT;

}
public void getInit()
{
    findViewById(R.id.btn).setOnClickListener(this);
    findViewById(R.id.btn2).setOnClickListener(this);

    start = (Button) findViewById(R.id.btn3);
    clear = (Button) findViewById(R.id.btn4);
    start.setOnClickListener(this);
    clear.setOnClickListener(this);
}



        @Override
        public void onClick(View v) {
            switch(v.getId())
            {

        case R.id.btn:
            new AlertDialog.Builder(this)
                    .setTitle("Info")
                    .setMessage("Battery Info © 2015 Natan Rosenfeld \nMy third Android application")
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // continue with delete
                        }
                    })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // do nothing
                        }
                    })
                    .show();
            break;
        case R.id.btn2:
            Intent startcredits = new Intent(this, CreditsActivity.class);
            startActivity(startcredits);
            break;
                case R.id.btn3:
                nmgr.notify(NOTIFICATION_ID,noti);
                break;
                case R.id.btn4:
                nmgr.cancel(NOTIFICATION_ID);


    }

}

}

Ypu 遇到了 NPE,因为您的 TextView 位于 activity_main.xml 并且您将 activity 设置为 activity_settings.xml。

因此,要么将 setContentView(R.layout.activity_settings); 更改为 setContentView(R.layout.activity_main);,要么在 activity_settings.xml

中添加一个 ID 为 batteryInfo 的 TextView

您可以使用 捆绑包 将数据从一个 activity 发送到另一个。

发送捆绑包。

Bundle bundle = new Bundle();
bundle.putString("Name",Object); //This is for a String
Intent i=new Intent(CurrentActivity.this, SecondActivity.class);
i.putExtras(bundle);
startActivity(i);

在另一个Activity

中接收捆绑包
Bundle bundle = null;
bundle = this.getIntent().getExtras();
String myString = bundle.getString("Name");//this is for String