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

时间:2015-03-15 18:36:13

标签: android string crash

我遇到了另一个问题。我试图在状态栏通知中显示电池信息,但是当我打开应用程序的设置时,我的应用程序崩溃了。我在这里不知所措,我不知道该做什么。 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);


    }

}

}

1 个答案:

答案 0 :(得分:1)

Ypu正在获取NPE,因为您的TextView位于activity_main.xml中,并且您使用activity_settings.xml设置活动。

因此,您可以将setContentView(R.layout.activity_settings);更改为setContentView(R.layout.activity_main);,或者只需在activity_settings.xml中添加id为batteryInfo的TextView

您可以使用捆绑将数据从一个活动发送到其他活动。

发送包。

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