2010-06-27 132 views
2

對不起,如果標題有點混亂,並且如果Android標籤不屬於我,我想我會添加它們,因爲這是Android應用程序。從內部類訪問類級別的東西

我想要做的是能夠從內部類RunningTimer中訪問Neoseeker類內部的對象neoApi。現在,在我的代碼中,你可以看到我想要的工作,但是當我運行我的應用程序時,沒有任何東西彈出。我的TextView裏沒有東西,沒有東西,什麼也沒有。我該如何補救?謝謝!

package com.neoseeker.android.app; 

import java.util.Timer; 
import java.util.TimerTask; 

import org.json.JSONObject; 

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Neoseeker extends Activity { 

    NeoAPI neoApi = new NeoAPI(); 
    Timer timer = new Timer(); 

    TextView text; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Set up Timer 
     timer.scheduleAtFixedRate(new RunningTimer(), 0, 1000 * 60 * 3); // Runs every 3 minutes 

     text = (TextView) findViewById(R.id.text); 

    } 

    /** 
    * Causes a status bar notification 
    * @param contentText The text to display to describe the notification 
    */ 
    public void causeNotification(CharSequence contentText) { 
     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
     int icon = R.drawable.icon; 
     CharSequence tickerText = "Neoseeker"; 
     long when = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, tickerText, when); 
     notification.defaults |= Notification.DEFAULT_ALL; 
     notification.defaults |= Notification.FLAG_AUTO_CANCEL; 

     Context context = getApplicationContext(); 
     CharSequence contentTitle = "Neoseeker"; 
     Intent notificationIntent = new Intent(); 
     PendingIntent contentIntent = PendingIntent.getActivity(Neoseeker.this, 0, notificationIntent, 0); 

     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
     int NEOSEEKER_ID = 125468131; // Random ID? 

     mNotificationManager.notify(NEOSEEKER_ID, notification); 
    } 

    class RunningTimer extends TimerTask { 

     boolean sent_notification = false; 
     int unread_pm_count_at_last_check = 0; 
     public void run() { 
      /* 
      int unread = Neoseeker.this.neoApi.getPmUnread(); 
      if (unread > 0) { 
       if (!sent_notification) { 
        Neoseeker.this.causeNotification("You have " + unread + " private message" + ((unread > 1) ? "s." ? ".")); 
       } 
      } 
      */ 
      int unread = Neoseeker.this.neoApi.getPmUnread(); 
      Toast.makeText(Neoseeker.this, "new: " + unread, Toast.LENGTH_SHORT).show(); 
      Neoseeker.this.text.setText("this is the text! " + unread); 
     } 
    } 
} 
+0

您是否嘗試過在run()中設置斷點以查看執行是否實際到達代碼? – 2010-06-27 06:34:06

+0

是的,我的代碼到達run()內部,所以我知道那不是問題。 – Chiggins 2010-06-29 17:31:46

回答

1

您應該在UI線程上執行UI修改調用。這是你的代碼速戰速決(RunningTimer的run()方法中):

final int unread = Neoseeker.this.neoApi.getPmUnread(); 
Main.this.runOnUiThread(new Runnable() { 

    public void run() { 

     Toast.makeText(Main.this, "new: " + unread, Toast.LENGTH_SHORT).show(); 
     Neoseeker.this.text.setText("this is the text! " + unread); 

    } 

}); 

更好的方式來做到這一點是低谷Handler

+0

Main是什麼?我複製並粘貼了您的快速修復,而Main在使用Eclipse時最終以紅色加下劃線。 – Chiggins 2010-06-29 17:29:30

+0

對不起,它應該是Neoseeker – ognian 2010-06-29 17:53:25

+0

哈,這很奇妙。謝謝你ognian :) – Chiggins 2010-06-29 18:05:20