Declaring TextView Error
final TextView nt=(TextView)findViewById(R.id.notify);
It says that this returns null, but indead it is in the R.java and the main and has text with it. I have no idea what is wrong, any help?
Error:
""=(No explicit return value)
Public void that uses 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, 0, toLaunch, 0);
notification.setLatestEventInfo(this, "Easy Settings", "Click to quickly access Easy Settings.", contentIntent);
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
notific开发者_运维知识库ation.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);
}
You need to call setContentView()
in your onCreate()
method before using findViewById()
. Here is an example of doing this...
@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);
}
Edit
Your nt
variable is not in the same scope as your addNotification()
method. Fix this by making it a class member or passing it into your addNotification()
method.
Also, to compare strings you should really be using .equals()
not ==
. ==
checks to see if it is the same object whereas .equals()
will match the text between two strings.
Edit (again)
I'm going to assume you already have nt
at the class level there's no way this would compile if not. Judging by what you've said, it seems your problem stems from the fact that you're creating a separate nt
variable in your onCreate()
. Change your code to something like the following (I've stripped out the unimportant parts).
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...
}
}
}
If this doesn't get you started then I'm not sure what the issue is. If this does not fix your problem you should post what you get from your logcat
.
I assume you have declared a field declared TextView nt
.
In your onCreate
method, you create another (local) variable nt, storing the TextView in this local variable, not in the global field.
Change this:
final TextView nt=(TextView)findViewById(R.id.notify);
into this:
nt = (TextView) findViewById(R.id.notify);
You can only use findViewById()
after you called setContentView()
.
Declare the nt
variable outside the onCreate()
method. You're declaring it as a local variable, so no other method will be able to see it.
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);
}
Look if your textview is in your main layout and not in another . And can you put some log please ?
精彩评论