2011-05-30 147 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是否在你的主佈局,而不是在另一個。你可以把一些日誌嗎?