2011-05-30 148 views
0
final TextView nt=(TextView)findViewById(R.id.notify); 

它说,这将返回null,但indead它是在R.java和主,并与它的文本。我不知道什么是错,有什么帮助?声明TextView错误

错误:

使用NT
""=(No explicit return value) 

公共无效:

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; 
     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); 
} 
+1

findViewById在使用setContentView()方法在onCreate()方法中声明时查找视图。你确定你做得对吗?也许更多的代码可以帮助。 – nhaarman 2011-05-30 17:06:39

回答

1

在使用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()将匹配两个字符串之间的文本。

编辑(再)

我会假设你已经在类级别有nt没有办法,这将汇编如果不是。根据你所说的话来看,你的问题似乎源于你在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... 
     } 
    } 
} 

如果这不能让你开始,那么我不知道是什么问题。如果这不能解决你的问题,你应该发布你的logcat

+0

我有一个OnCreate方法之外的公共void ...使用nt。 – Test2e31234234 2011-05-30 17:18:00

+0

编辑你的问题,并发布一些代码,显示你正在尝试做什么... – Bryan 2011-05-30 17:21:57

+0

通过回答编辑,以反映你刚刚发布的内容。 – Bryan 2011-05-30 17:39:12

0

您只能使用findViewById()你叫setContentView()后。

+0

如果是这样我可以放置它?因为当我将它放在它下面时,void不能读取变量。 – Test2e31234234 2011-05-30 17:14:37

1

我假设你已经声明了一个字段,声明为TextView nt

在您的onCreate方法中,您将创建另一个(本地)变量nt,将TextView存储在此局部变量中,而不是存储在全局字段中。

更改此:

final TextView nt=(TextView)findViewById(R.id.notify); 

成这样:

nt = (TextView) findViewById(R.id.notify); 
+0

是的,但第三次我需要在OnCreate方法 – Test2e31234234 2011-05-30 17:21:15

+0

以外的公共void的nt textview也得到一个不同的错误,当我把它放在setContent – Test2e31234234 2011-05-30 17:22:22

+0

之后因为onCreate()通常应该在任何其他方法之前被调用,它应该仍然有效。请给出更多的代码。帮助我们来帮助你。 – nhaarman 2011-05-30 17:22:55

0

声明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); 
} 
+0

这是有效的,但由于一些奇怪的原因,它仍然给我一个愚蠢的错误虐待后我错误代码更新 – Test2e31234234 2011-05-30 17:34:37

+0

你可以请teamview和我我需要帮助! ID - 624 264 021 PASS - 7783 – Test2e31234234 2011-05-30 17:45:37

0

看看你的textview是否在你的主布局,而不是在另一个。你可以把一些日志吗?