gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:51:58 25 4
gpt4 key购买 nike

我遇到了另一个问题。我试图在状态栏通知中显示电池信息,但是当我打开应用程序的设置时,我的应用程序崩溃。我在这里茫然,我不知道该怎么办。日志说:

    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_settings.xml设置 Activity 。

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

您可以使用 bundle 将数据从一个 Activity 发送到另一 Activity 。

发送 bundle 包。

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 bundle = null;
bundle = this.getIntent().getExtras();
String myString = bundle.getString("Name");//this is for String

关于android - Android Studio-无法在通知中显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29064610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com