如何解决声明TextView错误
|final TextView nt=(TextView)findViewById(R.id.notify);
它说这返回null,但实际上它在R.java和main中并带有文本。我不知道怎么了,有什么帮助吗?
错误:
\"\"=(No explicit return value)
使用NT的公共无效对象:
public void addNotification() {
if (nt.getText()==(\"yes\")) {
NotificationManager notifier = (NotificationManager)this.
getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon4;
Notification notification = new Notification(icon,\"Easy Settings Has Started\",System.currentTimeMillis());
Intent toLaunch = new Intent(this,EasySettings.class);
PendingIntent contentIntent =
PendingIntent.getActivity(this,toLaunch,0);
notification.setLatestEventInfo(this,\"Easy Settings\",\"Click to quickly access Easy Settings.\",contentIntent);
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
notification.flags |= Notification.DEFAULT_SOUND;
notifier.notify(0x007,notification);
} else {
if (nt.getText()==(\"no\")) {
//do nothing
}
}
}
OnCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView nt=(TextView)findViewById(R.id.notify);
try {
checkSB();
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
checkRing();
checkWifi();
checkBt();
AdView adview = (AdView)findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(false);
adview.loadAd(re);
}
解决方法
在使用
findViewById()
之前,您需要在onCreate()
方法中调用setContentView()
。这是执行此操作的示例...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set this to whatever your layout is
setContentView(R.layout.main);
// Now you can call the activity\'s findViewById() method...
TextView nt = (TextView)findViewById(R.id.notify);
}
编辑
您的nt
变量与addNotification()
方法的作用域不同。通过使其成为类成员或将其传递给addNotification()
方法来解决此问题。
另外,要比较字符串,您实际上应该使用.equals()
而不是==
。 ==
检查它是否是同一对象,而.equals()
将匹配两个字符串之间的文本。
再次编辑
我要假设您已经在课程级别上拥有ѭ8there,否则无法编译。从您所说的话来看,您的问题似乎源于您在onCreate()
中创建单独的nt
变量的事实。将您的代码更改为以下内容(我已经删除了不重要的部分)。
public class YourActivity extends Activity {
private TextView nt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Do not create a new instance,use the one you have...
nt = (TextView)findViewById(R.id.notify);
// Do whatever you were doing...
}
public void addNotification() {
// This *should* work...
if (nt.getText().equals(\"yes\")) {
// Do whatever you were doing...
}
else if (nt.getText().equals(\"no\")) {
// Do nothing...
}
}
}
如果这不能帮助您入门,那么我不确定是什么问题。如果这样做不能解决您的问题,则应张贴您的ѭ19get所得。
, 我假设您已声明一个声明为TextView nt
的字段。
在onCreate
方法中,创建另一个(局部)变量nt,将TextView存储在此局部变量中,而不是在全局字段中。
更改此:
final TextView nt=(TextView)findViewById(R.id.notify);
到这个:
nt = (TextView) findViewById(R.id.notify);
, 致电setContentView()
后,您才可以使用findViewById()
。
, 在onCreate()
方法之外声明nt
变量。您将其声明为局部变量,因此没有其他方法可以看到它。
TextView nt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nt=(TextView)findViewById(R.id.notify);
try {
checkSB();
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
checkRing();
checkWifi();
checkBt();
AdView adview = (AdView)findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(false);
adview.loadAd(re);
}
, 查看您的textview是否在主布局中,而不在其他布局中。你能放些日志吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。